package main
import "fmt"
type Persion struct {
name string
}
type Student struct {
Persion
age int
}
func (p *Persion) sayName() {
fmt.Println("我叫=", p.name)
}
func (p Persion) sayName2() {
fmt.Println("我叫", p.name)
}
func (p Student) sayName2() {
fmt.Println("我不叫", p.name)
}
func main() {
s1 := Student{Persion{"huhua"}, 19}
fmt.Println("s1=", s1)
//01继承可以调用父类中的方法
s1.sayName()
//02方法的重写
s1.sayName2()
//03显示调用父类的方法
s1.Persion.sayName2()
//04方法值 调用函数时无需传递接收者,隐藏了接收者
pFunc := s1.sayName
pFunc()
//05方法表达式 把接收者传递过去 //换种方法调用而已
f := (*Student).sayName
f(&s1)
}
4.6 go方法的继承和重写
猜你喜欢
转载自blog.csdn.net/h4241778/article/details/105318056
今日推荐
周排行