Golang 的结构体的组合(实现java继承的特性)
参考解析
题目来源: 大疆
答案:
- 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",
}