如下代码
package main
import "fmt"
type Course struct {
teacher Teacher //如果信息量比较多,怎么办,将另一个结构体的变量放进来
name string
price int
url string
}
type Teacher struct {
Name string
Age int
Title string
}
func (t Teacher) teacherInfo() {
fmt.Printf("姓名:%s,年龄:%d,职称:%s", t.Name, t.Age, t.Title)
}
func (c Course) courseInfo() {
fmt.Printf("课程名:%s,价格:%d,讲师信息:%s %d %s", c.name, c.price, c.teacher.Name, c.teacher.Age, c.teacher.Title)
}
//这种继承的效果很取巧
func main() {
//组合实现继承
t := Tearcher{
Name: "bobby",
Age: 18,
Title: "imooc",
}
c := Course{
teacher: Tearcher{
Name: "bobby",
Age: 18,
Title: "imooc",
},
name: "django",
price: 100,
url: "https://www.imooc.com",
}
}