package main
import "fmt"
// 定义Person
type Person struct {
name string
}
// 定义方法
func (p *Person) show() {
fmt.Println("name=" + p.name)
p.print()
}
func (p *Person) print() {
fmt.Println("Person print()")
}
// 定义Student
type Student struct {
Person
stuNo string
}
/*
//定义方法
func (s * Student) show() {
fmt.Println("name = " + s.name + ", stuNo=" + s.stuNo)
s.print()
}
*/
//定义方法
func (s * Student) print() {
fmt.Println("Student print()")
}
func main() {
p := Person{"chf"}
p.show()
s := Student{Person{"chf"}, "001"}
s.show()
}
Go的组合继承
最新推荐文章于 2023-07-27 16:55:11 发布