go继承(代码片段)

watermelonjuice watermelonjuice     2023-05-06     510

关键词:

go中没有继承,只能通过组合来实现继承。

继承和组合区别

继承就是子类继承了父类的特征和行为,使得子类实例具有父类的行为和方法,属于is-a的范畴。
组合就是通过对现有对象的拼装从而获得实现更为复杂的行为的方法。

  • 一个struct嵌套了另外一个匿名的struct从而实现了继承,嵌套多个匿名struct实现多重继承。
  • 一个struct嵌套了宁外一个struct的实例实现了组合。
type Animal  struct 



//继承
type Cat struct 
    //匿名
    *Animail


//组合
type Dog struct 
    animal Animal

继承的简单实现

type Animal struct 
    name string


type MachineCat struct 
    *Animal


//定义一个receive为IAnimal的函数
func (value *Animal) GetName() 
    fmt.Printf("Animal: %v
", value.name)


func main() 
    //实例化machineCat
    machineCat := &MachineCat
        &Animal
            "test",
        ,
    

    machineCat.GetName()


输出内容
Animal: test

声明一个struct Animal,然后再申明一个嵌套Animal的MachineCat的struct,再定义一个接受类型为Animal的GetName()的方法,当传入类型为MachineCat的时候,会先去找有没有传入类型为MachineCat的GetName()的方法,没有找到,就会继续寻找它的嵌入类型Animal的GetName()的方法。

如果申明一个传入类型为MachineCat会发生什么情况

在main()前面加入
//定义一个receive为MachineCat的函数
func (value *MachineCat) GetName() 
    fmt.Printf("MachineCat: %v
", value.name)


输出内容
MachineCat:test

可以看到调用的是receive为MachineCat的GetName()方法。

构造函数与多态

其他语言都是通过继承接口(实现一类行为的方法)来实现多态。

type IAnimal interface 
    GetName()


type Animal struct 
    Name string


func NewAnimal(name string) *Animal
    return &Animal
        Name: name,
    


func (a *Animal) GetName() 
    fmt.Printf("Animal name: %v
", a.Name)


type MachineCat struct 
    * Animal


func newMachineCat(name string) *MachineCat 
    return &MachineCat
        NewAnimal(name),
    


func main() 
    //实例化machineCat
    machineCat := newMachineCat("newMachineCat")

    var animal IAnimal = machineCat
    animal.GetName()

在go中,构造函数实际上就是一个返回为struct的普通函数。

首先定义一个IAnimal interface的接口,接口中只有一个声明为GetName()的方法。分别定义Animal, MachineCat的struct以及他们的构造方法。 只需要在外面传入参数,就可以生成各自的实例。在main()方法中,构造一个MachineCat赋值给接口对象var animal IAnimal就可以在接口中实现多态。如果调用MachineCat的GetName()的方法:

//receive类型为MachineCat的GetName
func (m *MachineCat) GetName() 
    fmt.Printf("MachineCat Name: %v
", m.Name)


输出内容
//MachineCat Name: newMachineCat

如果需要再MachineCat对象调用Animal的方法

machineCat.Animal.GetName()

输出内容
Animal name: newMachineCat

由此可见Animal是machineCat的一部分,可以直接调用成员的方法。

多态的参数

多态的一个主要应用是传入的类型为父类对象,在实例化使用的时候,调用的方法由传入对象的实例决定。

func check(animal IAnimal) 
    animal.GetName()


check(machineCat)

输出内容
MachineCat Name: newMachineCat

多重继承

type Animal struct 
    Name string


type Machine struct 
    MachineName string


type MachineCat struct 
    *Animal
    *Machine


//receive类型为Animal的GetName
func (a *Animal) GetName() 
    fmt.Printf("Animal name: %v
", a.Name)


//receive类型为Machine的Print
func (m *Machine) Print() 
    fmt.Printf("Machine name: %v
", m.MachineName)


func main() 
    //实例化machineCat
    machineCat := &MachineCat
        &Animal
            Name: "machine animal name",
        ,
        &Machine
            MachineName: "machine name",
        ,
    

    machineCat.GetName()
    machineCat.Print()


输出内容
Animal name: machine animal name
Machine name: machine name

可以看到多态继承和单继承的区别就是组合中嵌套了更多的匿名struct。可以看到在子类(名义上)中分别继承了Animal的GetName()方法和Machine的Print()的方法。

如果多重继承中,父类实现了相同的方法,会发生什么情况呢?
代码中Machine添加一个GetName()的方法。

//receive类型为Machine的GetName
func (m *Machine) GetName() 
    fmt.Printf("Machine name: %v
", m.MachineName)


出现错误:
ambiguous selector machineCat.GetName

在多重继承的父类有相同的方法的时候,就会出现子类出现模糊不清的调用,编译器无法通过。

总结

  1. go继承是通过嵌套匿名struct实现继承。
  2. go继承在本质上还是组合。
  3. 子类要调用父类的实现可以通过调用组合中的父类对象的方法。
  4. 多重继承中不允许多个父类出现相同的方法。

第20课go如何实现继承的(代码片段)

golang语言中没有继承,但是可以依靠组合来模拟继承和多态。packagecontrollersimport("encoding/json"md"gowebProject/models"//给引用的模块起个别名"github.com/astaxie/beego")typeUserControllerstructbeego.Controller//通过这种组合来实现继承func(c*UserC 查看详情

go继承(代码片段)

go中没有继承,只能通过组合来实现继承。继承和组合区别继承就是子类继承了父类的特征和行为,使得子类实例具有父类的行为和方法,属于is-a的范畴。组合就是通过对现有对象的拼装从而获得实现更为复杂的行为的方法。一... 查看详情

两分钟让你明白go中如何继承(代码片段)

...,抽象了大量的接口。也使用这些抽象的接口做了很多伪继承的操作,极大的减少了代码冗余,同时也增加了代码的可读性。然后随便搜了一下关于Go继承的文章,发现有的文章的代码量过多,并且代码format极其粗糙,命名极其... 查看详情

#yyds干货盘点#愚公系列2022年08月go教学课程032-结构体方法继承(代码片段)

一、结构体方法继承1.继承的概念继承是指一个子类(或称为派生类)继承父类(或称为基类)的特征(属性和操作)。继承是面向对象程序设计时实现代码复用的重要手段,它允许在原有的类的基础上进行扩展,增加功能,这样新产生... 查看详情

go语言自学系列|golang继承(代码片段)

视频来源:B站《golang入门到项目实战[2021最新Go语言教程,没有废话,纯干货!持续更新中...]》一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!附上汇总贴ÿ... 查看详情

go中匿名字段的方法继承与方法重写(代码片段)

//code_019_struct_anonymous_field_methodprojectmain.gopackagemainimport("fmt")typePersonstructnamestringsexbyteageintfunc(p*Person)PrintInfo()fmt.Printf("Person:%s,%c,%d",p.name,p.sex,p.age)type 查看详情

go语音之进阶篇接口的继承(代码片段)

1、接口的继承示例:packagemainimport"fmt"typeHumanerinterface//子集 sayhi()typePersonerinterface//超集 Humaner//匿名字段,继承了sayhi() sing(lrcstring)typeStudentstruct namestring idint//Student实现了sayhi()func(tmp*Student)sayhi() fmt.Printf("Student[%s,%d]sayhi",tmp.name,tm... 查看详情

go中的"继承"之struct的匿名字段2(代码片段)

//code_016_struct_anonymous_field2projectmain.gopackagemainimport("fmt")typePersonstructnamestringsexbyteageinttypeStudentstructPersonidintaddrstringnamestring//同名字段typemystrstring//自定义类型typeStude 查看详情

go结构体嵌套和用结构体实现模拟“继承”(代码片段)

什么是结构体嵌套一个结构体中可以嵌套包含另一个结构体或结构体指针结构体嵌套packagemainimport"fmt"//桌子结构体typetablestruct materialstring shapeint comcommon//嵌套结构体//被嵌套的结构体定义typecommonstruct yearintfuncmain() //嵌套结... 查看详情

go中的"继承"之struct的匿名字段(代码片段)

//code_016_anonymous_fieldprojectmain.gopackagemainimport("fmt")typePersonstructnamestringsexbyteageinttypeStudentstruct//一般情况下,定义结构体的时候,字段名和类型一一对应,如下:Person为类型,并无字段名。Person//匿名字段,name默认Student就包含了 查看详情

原型链与继承(代码片段)

  一、原型链    1.构造函数varAnimal=function(name)this.name=namethis.age=‘22‘this.run=function()returnname+‘isrunning...‘Animal.prototype.go=function()returnthis.name+‘go‘varcat=newAnimal(‘cat‘)vardog=ne 查看详情

go面向对象(代码片段)

目录面向对象介绍匿名字段什么是继承?指针类型匿名字段多重继承方法什么是封装?方法创建接口接口定义空接口类型断言综合示例接口作为函数参数接口嵌套面向对象介绍面向对象和面向过程都是解决问题的一种思路。面向... 查看详情

继承复习-go(代码片段)

继承通过在结构体中嵌套实现继承,且继承的只是结构体的属性,若要继承方法可以通过实现接口达成接口多态在golang中interface相当于类,struct相当于对象,当struct重写了interface的方法就是struct继承了interface类的... 查看详情

[go]匿名字段的同名字段操作(代码片段)

...peStudentstructPerson//只有名字,没有字段,这里student相当于继承了person的所有字段,就有点像是继承了idintaddrstringnamestring//这里跟person里的字段同名了,那么被使用的时候,操作是studnet还 查看详情

『golang』面向对象(代码片段)

...的实现。面向对象语言最重要的三个方面分别是:封装,继承和多态,在Go中它们是怎样表现的呢?Go实现面向对象的两个关键是struct和interface,结构代替类,因为Go语言不提供类,但提供了结构体或自定义类型,方法可以被添加... 查看详情

go-05-常量(代码片段)

...3.14159263声明多个常量const(codeOkint=200conde404=404)4值的继承当声明常量时,若果后面的常量没有指定具体的值,就会继承上一个常量 查看详情

go基础:方法(代码片段)

...针或值作为接收者4方法和未导出字段5内嵌类型的方法和继承6如何在类型中嵌入功能7多重继承8通用方法和方法命名9和其他面向对象语言比较Go的类型和方法备注问题1问题21方法是什么在Go语言中,结构体就像是类的一种简化... 查看详情

go接口/错误处理知识点(代码片段)

接口面向对象3大特性,封装/继承/多态,封装采用结构体完成,继承使用组合的方式完成,而接口使用的是所谓的鸭子模型(DuckType)。也就是说不像c++/java中,实现接口必须继承实现,只需要实现了接... 查看详情