package main
import (
"fmt"
)
type base struct {
num int
}
func (b base) describe() string {
return fmt.Sprintf("base with num=%v", b.num)
}
// 类似js的原型链原理
type container struct {
base // 继承
str string
}
type describer interface { // 接口
describe() string
}
func describeFn(d describer) { // 接口方法
fmt.Println(d.describe())
}
func main() {
co := container{
base: base{
num: 1,
},
str: "something",
}
fmt.Println(co.num, co.str) // 直接查找属性和方法
fmt.Println(co.base.num)
fmt.Println(co.describe()) // base的方法也是co的方法,因为co继承了base。
describeFn(co)
}