Golang 的结构体的组合(实现java继承的特性)

本文阅读 1 分钟
首页 golang 正文
题目序号:(267)
题目来源: 大疆
频次: 1

答案:阿纪、

  1. golang 通过结构体嵌套实现继承的特性
    在Go语言里,没有面向对象这个概念,自然就没有继承,但它支持结构体组合;
    你可以通过在结构体内嵌套结构体实现组合;

    type animal struct {
        name string
        age  string
    }
    
    type cat struct {
        animal
        sound string
    }
    
    type dog struct {
        animal
        color string
    }
  2. 使用
cat := cat{
    animal: animal{
        name: "xiaomiao",
        age:  "1",
    },
    sound: "miaomiaomiao",
}
dog := dog{
    animal: animal{
        name: "dahuang",
        age:  "2",
    },
    color: "yellow",
}  
本文来自投稿,不代表本站立场,如若转载,请注明出处:
syncpool的实现原理
« 上一篇 09-17
问了sync.Map(我说我对sync.Pool比较熟,就说Pool了)
下一篇 » 09-17

发表评论

发表评论