Go 继承,多继承

demo.go(多继承):

package main

import "fmt"

// 定义结构体类型 (父类1)
type Person1 struct {
	id   int
	name string
}

// 定义结构体类型 (父类2)
type Person2 struct {
	age  int
}

// 定义结构体类型 (子类)
type Student struct {
	Person1     // 继承
	Person2     // 多继承
	score int
}

func main() {
	// 对象
	var stu Student
	stu.id = 2
	stu.name = "张三"
	stu.age = 20
	stu.score = 90

	fmt.Println(stu)  // {{2 张三} {20} 90}
}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/88694164