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
}