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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了