一、对象与继承的声明
1.1 定义一个简单的对象 People, 包括性别、年龄、身高
type People struct {
Gender string
Age int
Height int
}
1.2 基于people对象,再定义 Student、Engineer 对象
Student 对象继承people, 有自己的属性 “course”
Engineer 对象继承people, 有自己的属性 “Job、Experience”
type Student struct {
People
Course string
}
type Engineer struct {
People
Job string
Experience int
}
二、对象与继承的赋值
2.1 变量的引用
var studyPeople []People
student := new(Student)
studyPeople = getStudyPeopleList()
for _, people := range studyPeople {
student.People = people
student.Course = "English"
}
var workingPeople []People
engineer := new(Engineer)
workingPeople = getWorkingPeopleList()
for _, people := range workingPeople {
engineer.People = people
engineer.Job = "Coding"
engineer.Experience = 3
}