go 类和继承
作者:互联网
类和继承
//Go通过结构体构建类
type Person struct {
name string
age int
gender string
score float64
}
func (p *Person)Eat() {
//类绑定方法
p.name = "hello"
fmt.Println("eat")
}
func (p Person)Eat2() {
p.name="world"
fmt.Println("eat2")
}
//类的继承
type Hum struct {
name string
age int
gender string
}
func (h *Hum)Eat() {
fmt.Println(h.name)
fmt.Println("方法")
}
type Student1 struct {
hum Hum
//类的嵌套
score float64
}
type Teacher struct {
Hum
//类的继承
subject string
}
标签:struct,继承,Hum,fmt,name,go,type,string 来源: https://www.cnblogs.com/aiverhua/p/16280116.html