golang学习的点点滴滴:struct匿名字段
发布时间:2020-12-16 18:49:14 所属栏目:大数据 来源:网络整理
导读:packagemainimport"fmt"typeBasestruct{NamestringAgeint}typeChildstruct{Base//匿名字段,默认把Base的所有字段都继承过来了。这样看起来才像真正的继承Ageint}funcmain(){c:=new(Child)c.Name="hello"//可以直接使用Base中的字段c.Age=20//如果有重复的,
packagemain import"fmt" typeBasestruct{ Namestring Ageint } typeChildstruct{ Base//匿名字段,默认把Base的所有字段都继承过来了。这样看起来才像真正的继承 Ageint } funcmain(){ 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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |