|
- package main
- import "fmt"
- func main() {
- //1.创建Person类型
- p1 := Person{name: "王二狗", age: 30}
- fmt.Println(p1.name, p1.age)
- p1.eat()
- //2.创建Student类型
- s1 := Student{Person{"Ruby", 18}, "千峰教育"}
- fmt.Println(s1.name)
- fmt.Println(s1.age)
- fmt.Println(s1.school)
- s1.eat()
- s1.study()
- s1.eat()
- }
- // 1.定义一个“父类”
- type Person struct {
- name string
- age int
- }
- // 2.定义一个“子类”
- type Student struct {
- Person
- school string
- }
- // 3.方法
- func (p Person) eat() {
- fmt.Println("父类的方法,吃窝窝头。。。")
- }
- func (s Student) study() {
- fmt.Println("子类新增的方法,学生学习啦。。。")
- }
- func (s Student) eat() {
- fmt.Println("吃炸鸡喝啤酒。。。。")
- }
复制代码
|
|