- 重写
package main
import "fmt"
type person struct {
id int
name string
}
type student struct {
person
class int
}
func (p *person8) Say() {
fmt.Printf(p.name)
}
//子类对象和父类对象的方法名重名
func (s *student8) Say() {
fmt.Printf(s.class)
}
func main() {
stu := student8{person8{1001, "wang"}, 112,}
//子类对象方法 采用就进原则 使用子类对象方法
stu.Say()
//父类对象方法
stu.person.Say()
fmt.Println(stu.SayHello)
fmt.Println(stu.person8.SayHello)
}
本文介绍了一个Go语言中结构体与方法重写的示例,展示了如何定义结构体person和student,以及如何在子类student中重写父类person的方法Say。通过实例演示了子类对象方法调用的就近原则。
366

被折叠的 条评论
为什么被折叠?



