dpda53918 2013-07-31 13:57
浏览 97
已采纳

Go是否支持继承?

I have heard a lot of people talk about Go, and how it does not support inheritance. Until actually using the language, I just went along with the crowd and listened to the hear say. After a little messing about with the language, getting to grips with the basics. I came across this scenario:

    package main

type Thing struct {
    Name string
    Age  int
}

type UglyPerson struct {
    Person
    WonkyTeeth bool
}

type Person struct {
    Thing
}

type Cat struct {
    Thing
}


func (this *Cat) SetAge(age int){
    this.Thing.SetAge(age)
}

func (this *Cat GetAge(){
     return this.Thing.GetAge() * 7
}

func (this *UglyPerson) GetWonkyTeeth() bool {
    return this.WonkyTeeth
}

func (this *UglyPerson) SetWonkyTeeth(wonkyTeeth bool) {
    this.WonkyTeeth = wonkyTeeth
}

func (this *Thing) GetAge() int {
    return this.Age
}

func (this *Thing) GetName() string {
    return this.Name
}

func (this *Thing) SetAge(age int) {
    this.Age = age
}

func (this *Thing) SetName(name string) {
    this.Name = name
}

now, what this does it composes the Person and Cat Structs, from the Thing Struct. By doing so, not only does the Person and Cat struct share the same Fields as the Thing Struct, but also, through composition, the methods of Thing are also shared. Is this not inheritance? Also by implenting an interface as such:

type thing interface {
    GetName() string
    SetName(name string)
    SetAge(age int)
}

All three Structs are now joined or should I say, can be used in a homogenous fashion, such as an array of "thing".

So, I lay it on you, is this not inheritance?

Edit

Added a new derived Struct called "Ugly person" and Overridden the SetAge method for Cat.

  • 写回答

3条回答 默认 最新

  • dream890110 2013-07-31 14:10
    关注

    It is inheritance but probably not the sort of inheritance you are probably after. Your example look promising b/c Person and Cat are behaviorally and structurally equal to each other modulo the type names.

    As soon as you'd attempt to use this "inheritance" to 'extend' some base type with, for example added fields, you'll find that the receiver of the "base class" is always the base class, never the extended one. IOW, you cannot achieve structurally polymorphous type hierarchy.

    OTOH, Go supports purely behavioral inheritance via interfaces. Embedding one interface into another does create an inheritance tree.

    package main
    
    import "fmt"
    
    type Thing struct {
        Name string
        Age  int
    }
    
    func (t *Thing) me() {
        fmt.Printf("I am a %T.
    ", t)
    }
    
    type Person struct {
        Thing
    }
    
    func (p *Person) Iam() {
        fmt.Printf("I am a %T.
    ", p)
    }
    
    type Cat struct {
        Thing
    }
    
    func (c *Cat) Iam() {
        fmt.Printf("I am a %T.
    ", c)
    }
    
    func main() {
        var p Person
        var c Cat
    
        p.me()
        p.Iam()
    
        c.me()
        c.Iam()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!