美文网首页
匿名对象

匿名对象

作者: BridgeXD | 来源:发表于2019-04-17 20:15 被阅读0次
    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)
    }

    相关文章

      网友评论

          本文标题:匿名对象

          本文链接:https://www.haomeiwen.com/subject/ednoiqtx.html