匿名对象

2019-04-17  本文已影响0人  BridgeXD
type Person struct {
id   int
name string
age  int
}

type Student struct {
  Person // 匿名字段实现继承关系
     // *Person //指针作为匿名字段
  class  string
  score  int
}

func main() {
  var stu Student
  //需要对指针创建空间
  //stu.Person = new(Person)
  stu.id = 99
  stu.age = 11
  stu.name = "sd"
  stu.class = "yi"
  stu.score = 22
  fmt.Println(stu)
}
上一篇 下一篇

猜你喜欢

热点阅读