diff --git a/base/struct/inheritance/main.go b/base/struct/inheritance/main.go new file mode 100644 index 0000000..8fa4619 --- /dev/null +++ b/base/struct/inheritance/main.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +// 结构体 实现 类似 其他语言的 继承 + +// Animal 动物 +type Animal struct { + Name string +} + +func (a *Animal) Move() { + fmt.Printf("%s: 移动\n", a.Name) +} + +// Dog 狗 +type Dog struct { + Animal // Animal 拥有的方法, Dog 也拥有 + Feet uint8 +} + +func (d *Dog) Cell() { + fmt.Printf("%s: 叫\n", d.Name) +} + +func main() { + d1 := &Dog{ + Animal: Animal{Name: "大黄"}, + Feet: 4, + } + + fmt.Printf("%#v\n", d1) + //d1.Animal.Move() + d1.Move() + d1.Cell() +}