Golang 匿名字段

时间:2015-01-31 22:01:41   收藏:0   阅读:204
// code by shaoyongyang
package main

import (
    "fmt"
)

type People struct {
    name   string
    age    int
    weight int
}

type Student struct {
    People
    specialty string
}

func main() {
    liming := Student{People{"liming", 18, 183}, "None Specialty"}
    fmt.Println(liming.name)
    liming.People.name = "xiaohong"
    fmt.Println(liming.name)
}


其实就是一个结构体,嵌入了另一个结构体。 对应的修改数据方式方式,以及数据访问的方式跟着改变。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!