go语言--struct继承和聚合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/enjoy_sun_moon/article/details/85134550

type person {name string

sex int,

age int,

}

type student {

       person,                   用匿名字段来模拟继承

       grade string,

      school string,

}

以上这种类型是继承类型  人包括学生  两者是有关联的

type Address struct{
    shi,sheng string
}
type Student struct{
    
    addr Address
    name,color string
}
func main(){
    p:=Student{Address{"shijaiz","hebei"},"聂伟博","蓝哥"}
    fmt.Printf("%+v",p)

    
    a := Student{}
    a.addr = Address{"sfds","sfsdfd"}
    a.name = "大幅度"
    a.color= "fdsafsd"
    fmt.Printf("%+v",a)

猜你喜欢

转载自blog.csdn.net/enjoy_sun_moon/article/details/85134550