package main
import "fmt"
type Base struct {
Name string
Age int
}
type Child struct {
Base // 匿名字段, 默认把Base的所有字段都继承过来了。 这样看起来才像真正的继承
Age int
}
func main() {
c := new(Child)
c.Name = "hello" // 可以直接使用Base中的字段
c.Age = 20 // 如果有重复的, 则最外的优先
fmt.Println(c.Name) // hello
fmt.Println(c.Age) // 20
fmt.Println(c.Base.Age) // 要访问Base中的,可以这样写 0
}
import "fmt"
type Base struct {
Name string
Age int
}
type Child struct {
Base // 匿名字段, 默认把Base的所有字段都继承过来了。 这样看起来才像真正的继承
Age int
}
func main() {
c := new(Child)
c.Name = "hello" // 可以直接使用Base中的字段
c.Age = 20 // 如果有重复的, 则最外的优先
fmt.Println(c.Name) // hello
fmt.Println(c.Age) // 20
fmt.Println(c.Base.Age) // 要访问Base中的,可以这样写 0
}