type person struct{
name string
age int
sex string}// 结构体嵌套结构体type Student struct{//通过匿名字段实现继承操作
person //结构体名称作为结构体成员
id int
score int}funcmain01(){var stu Student = Student{person{"张三丰",190,"男"},101,100}//stu.id=101结构体名称.父类成员信息//stu.name="张三"stu.person.name="张三"//stu.score=100//stu.sex="男"//stu.age=18
fmt.Println(stu)}
2.同名字段
type Person struct{
name string
age int
sex string}type student struct{
Person
id int
name string
score int}funcmain02(){var stu student=student{Person{"张三疯",18,"男"},102,"张三",100}//采用就进原则 使用子类信息//stu.name="张三丰"结构体变量.匿名字段.结构体成员stu.Person.name="张三疯"//stu.id=101//stu.score=99//stu.sex="男"//stu.age=18
fmt.Println(stu)}
3.指针匿名字段
type person1 struct{
name string
age int
sex string}type student1 struct{*person1 //指针匿名字段
id int
score int}funcmain03(){var stu student1 = student1{&person1{"小龙女",38,"女"},105,66}// var per = person1{"杨过", 35, "男"}//stu.person.name//将结构体变量 赋值给结构体指针类型// stu.person1 = &per// stu.person1 = new(person1)// stu.name = "郭襄"// stu.person1.name = "郭小姐"// stu.id = 101// stu.score = 78
fmt.Println(stu)
fmt.Println(stu.name)
fmt.Println(stu.id)
fmt.Println(stu.score)}
4.多重继承
type TestA struct{
name string
id int}type TestB struct{
TestA
sex string
age int}//注意结构体不能嵌套本结构体//结构体可以嵌套本结构体指针类型 链表type TestC struct{//*TestC ok//TestC//err
TestB
score int}funcmain04(){var s TestC=TestC{TestB{TestA{"姆巴佩",10},"男",19},4}//s.score=100//s.TestC.score=100//s.TestB.TestA.name="姆巴佩"//s.name="李四"//s.id=201//s.sex="男"//s.age=20//s.score=10
fmt.Println(s)}
5.多重继承应用
type DemoA struct{
name string
id int}type DemoB struct{
age int
sex string}type DemoC struct{
DemoA
DemoB
score int}funcmain05(){var s DemoC=DemoC{DemoA{"戈洛文",7},DemoB{17,"男"},6}//s.name="戈洛文"//s.score=6//s.age=17//s.id=7//s.sex="男"//
fmt.Println(s)}/*
type skills struct{
名称
耗蓝
CD 冷却时间
范围
伤害
}
定义结构体切片 保存技能信息
type role struct{
名称
等级 lv
经验 exp
钻石
金币
生命值 hp
攻击力
暴击
防御
蓝量mp
skills//匿名字段
s []skills //结构体切片
}
*//*
type 信用卡 struct{
卡号
持卡人姓名
额度
有效期
密码
银行信息
消费记录//匿名函数
记录 []消费记录
}
type 消费记录 struct{
卡号
消费时间
消费id
流水号
消费金额
备注
}
*/
6.方法定义和使用
////func add(a,b int) int{// return a+b//}//1、定义函数类型//2、为已存在的数据类型起别名type Int int//1、预处理 2、编译 3、汇编 4、链接//方法//func (方法接收者)方法名(参数列表)返回值类型func(a Int)add(b Int)Int{return a+b
}funcmain06(){//result:=add(10,20)//根据数据类型 绑定方法var a Int=10
value:=a.add(20)//var b Int
fmt.Println(value)}
7.方法地址传递与值传递
type Stu struct{
name string
age int
sex string}// 方法 值传递func(s Stu)PrintInfo(){
fmt.Println(s.name)
fmt.Println(s.age)
fmt.Println(s.sex)}// 地址传递func(s *Stu)EditInfo(name string, age int, sex string){//(*s).name=name//结构体指针可以直接调用结构体成员
s.name = name
s.age = age
s.sex = sex
//s.PrintInfo()}funcmain0702(){var s1 *Stu
s1 =new(Stu)
s1.EditInfo("薛宝钗",24,"女")
s1.PrintInfo()}funcmain0701(){//var s Stu = Stu{"刘姥姥", 54, "女"}对象.方法//s.PrintInfo()var s1 Stu = Stu{"王宝钗",24,"女"}
s1.PrintInfo()//(&s1).EditInfo("林黛玉",16,"女")//ok//结构体变量可以直接使用结构体指针对应的方法
s1.EditInfo("林黛玉",16,"女")
s1.PrintInfo()}
8.方法练习
type student2 struct{
name string
age int
sex string
cscore int
mscore int
escore int}// 为对象赋值func(s *student2)InitInfo(name string, age int, sex string, cscore int, mscore int, escore int){
s.name = name
s.age = age
s.sex = sex
s.mscore = mscore
s.cscore = cscore
s.escore = escore
}funcmain08(){//stu := student2{"贾宝玉", 18, "男", 66, 77, 88}var stu student2
//初始化对象信息
stu.InitInfo("贾宝玉",18,"男",66,77,88)SayHello()//stu.SayHello()
stu.PrintScore()}
type person3 struct{
id int
name string
age int
sex string}type student3 struct{
p person3 //结构体变量 结构体类型//person3
score int}func(p *person3)SayHello(){
fmt.Println("1大家好,我是", p.name,"我是", p.sex,"生,我今年", p.age)}func(s *student3)SayHello(){
fmt.Println("2大家好,我是", s.p.name,"我是", s.p.sex,"生,我今年", s.p.age)}funcmain10(){var stu student3 = student3{person3{203,"贾玲",18,"女"},100}var p person3 = person3{203,"贾玲",18,"女"}//父类不能继承子类信息
p.SayHello()//var stu student3=student3{100}//stu.p.age = 18//stu.p.name = "贾玲"//stu.p.sex = "女"//stu.p.id = 203//stu.score = 100//子结构体继承父类结构体 允许使用父类结构体成员 允许使用父类的方法
stu.p.SayHello()
stu.SayHello()//fmt.Println(stu)}
11.方法继承练习
/*
记者:我是记者 我的爱好是偷拍 我的年龄是34 我是一个男狗仔
程序员:我叫孙全 我的年龄是23 我是男生 我的工作年限是 3年
*/type Person1 struct{
name string
age int
sex string}type Rep struct{
Person1
hobby string}type Dep struct{
Person1
worktime int}func(p Person1)SayHello(){
fmt.Printf("大家好,我是%s,我是%s生,我今年%d岁了", p.name, p.sex, p.age)}func(r Rep)SayHi(){
r.SayHello()
fmt.Println("我的爱好是", r.hobby)}func(d Dep)SayHi(){
d.SayHello()
fmt.Println("我的工作年限是", d.worktime)}funcmain11(){var r Rep = Rep{Person1{"卓伟",40,"男"},"偷拍"}var d Dep = Dep{Person1{"汤姆逊",68,"男"},40}
r.SayHi()
d.SayHi()}
12.方法重写
type person4 struct{
name string
age int
sex string}type student4 struct{
person4
score int}func(p person4)PrintInfo(){
fmt.Printf("大家好,我是%s,我今年%d岁,我是%s生\n", p.name, p.age, p.sex)}// 方法重写 在一个对象中不能出现相同的方法名 方法的接收者 带* 和不带* 表示一个相同的对象func(s student4)PrintInfo(){
fmt.Printf("大家好,我是%s,我今年%d岁,我是%s生,我的分数是%d分\n", s.name, s.age, s.sex, s.score)}// func (s *student4)PrintInfo(){// fmt.Printf("大家好,我是%s,我今年%d岁,我是%s生,我的分数是%d分\n",s.name,s.age,s.sex,s.score)// }funcmain(){
s := student4{person4{"张三",11,"男"},19}//默认使用子类的方法 采用就进原则//调用子类方法
s.PrintInfo()//调用父类方法
s.person4.PrintInfo()}
13.方法类型和方法值
type person5 struct{
name string
age int
sex string}type student5 struct{
person5
score int}type funcdemo func()func(p *person5)PrintInfo(){
fmt.Printf("大家好,我是%s,我今年%d岁,我是%s生\n", p.name, p.age, p.sex)}// 方法重写 在一个对象中不能出现相同的方法名 方法的接收者 带* 和不带* 表示一个相同的对象func(s *student5)PrintInfo(name string){
s.name = name
fmt.Printf("大家好,我是%s,我今年%d岁,我是%s生,我的分数是%d分\n", s.name, s.age, s.sex, s.score)}funchello(){
fmt.Printf("hello")}funcmain13(){
s := student5{person5{"张三",11,"男"},19}//代码区//fmt.Println(s.PrintInfo)//fmt.Println(s.person5.PrintInfo)//fmt.Println(hello)//var f1 funcdemo////f1=s.PrintInfo//fmt.Printf("%T\n",f1)////f:=s.PrintInfo//fmt.Printf("%T\n",f)////f:=hello//fmt.Printf("%T\n",f)//f()//a:=10//fmt.Printf("%p\n",&a)//方法类型(函数类型) 变量
f := s.PrintInfo
f("李二狗")
fmt.Println(s)
fmt.Printf("%T\n", f)//f:=PrintInfo//err}