6.3. 第1节: 继承

未匹配的标注
本文档最新版为 2023,旧版本可能放弃维护,推荐阅读最新版!

1 继承

根据上面的图,我们发现学生类(结构体),讲师类(结构体)等都有共同的成员(属性和方法),这样就存在重复,所以我们把这些重复的成员封装到一个父类(结构体)中。然后让学生类(结构体)和讲师类(结构体)继承父类(结构体)
接下来,我们可以先将公共的属性,封装到父类(结构体)中实现继承,关于方法(函数)的继承后面再讲。

1.1匿名字段创建与初始化

那么怎样实现属性的继承呢?
可以通过匿名字段(也叫匿名组合)来实现,什么是匿名字段呢?通过如下使用,大家就明白了。

package main

import "fmt"

//结构体嵌套结构体
//父类
type Person struct {
    id   int
    name string
    age  int
    sex  string
}

//每一个类描述一个对象的属性
//子类
type Student struct {
    //将Person结构体作为student结构体的成员
    //p Person//为person结构体起名字
    Person //匿名字段实现继承关系

    class int
    score int
}

func main0101() {

    //创建对象
    var stu Student

    //student继承与父类person 可以直接使用父类的结构体成员
    //stu.Person.name = "张三"
    //stu.Person.age = 18
    //stu.Person.id = 1001
    stu.name = "张三"
    stu.age = 18
    stu.id = 1001
    stu.sex = "女"

    //stu.p.name
    //stu.p.age
    //stu.p.id
    //stu.p.sex
    stu.class = 302
    stu.score = 99

    fmt.Println(stu)

}
func main(){
    //如果一个结构体包含其他结构体信息 需要依次进行初始化
    var stu Student=Student{Person:Person{1001,"东方不败",35,"不祥"},score:100,class:302}
    fmt.Printf("姓名:%s\n",stu.name)
    fmt.Printf("性别:%s\n",stu.sex)
    fmt.Printf("年龄:%d\n",stu.age)
    fmt.Printf("编号:%d\n",stu.id)
    fmt.Printf("班级:%d\n",stu.class)
    fmt.Printf("成绩:%d\n",stu.score)
}

1.2成员操作

请看1.1

1.3同名字段

package main

import "fmt"

type person struct {
    id   int
    name string
    age  int
    sex  string
}

type student struct {
    person

    name  string
    class int
    score int
}

func main() {

    //对象初始化
    //var stu student
    //stu.score = 100
    //stu.class = 301
    //stu.id = 1002
    //stu.age = 18
    //stu.sex = "男"
    ////子类赋值
    ////子类和父类结构体有相同的成员名 默认赋值为子类 采用就近原则
    //stu.name = "瞎子"
    ////父类赋值
    //stu.person.name = "李青"
    stu:=student{person{1002,"李青",18,"男"},"李青",301,100}

    fmt.Println(stu)
}

1.4指针类型匿名字段

匿名字段成员为指针:

package main

import "fmt"

type person1 struct {
    id   int
    name string
    age  int
    sex  string
}

type student1 struct {
    *person1 //指针作为匿名字段

    class int
    score int
}

func main() {

    var stu student1

    stu.class = 301
    stu.score = 90

    //stu.person1是一个指针类型 默认值为nil 0x0
    //需要对指针进行创建空间  new(person1)
    //stu.person1=new(person1)
    ////(*stu.person1).name="盖伦"
    //stu.name = "盖伦"
    //stu.id = 1
    //stu.age = 30
    //stu.sex = "男"

    stu.person1 = &person1{1001, "盖伦", 30, "男"}

    //fmt.Println(stu)
    fmt.Printf("姓名:%s\n", stu.name)
    fmt.Printf("性别:%s\n", stu.sex)
    fmt.Printf("年龄:%d\n", stu.age)
    fmt.Printf("编号:%d\n", stu.id)
    fmt.Printf("班级:%d\n", stu.class)
    fmt.Printf("成绩:%d\n", stu.score)
}

1.5多重继承

package main

import "fmt"

type person2 struct {
    name string
    age  int
    sex  string
}
type person3 struct {
    id   int
    addr string
}

type student2 struct {
    //结构体成员为多个匿名字段
    person2
    person3

    class int
    score int
}

func main() {
    //var stu student2
    //
    //stu.person3.id = 1001
    //stu.person2.name = "亚索"
    //stu.age = 10
    //stu.score = -5
    //stu.class = 300
    //stu.addr = "召唤师峡谷"
    //stu.sex = "男"

    stu:=student2{person2{"劫",12,"男"},person3{1002,"召唤师峡谷"},300,-5}

    fmt.Println(stu)

}

代码2:


package main

import "fmt"

type human struct {
    id   int
    name string
}
type person4 struct {
    human

    age  int
    sex  string
    addr string
}

type student4 struct {
    person4

    class int
    score int
}

func main0501() {
    //var stu student4
    //
    //stu.name = "魏璎珞"
    //stu.sex = "女"
    //stu.addr = "皇宫"
    //stu.class = 3004
    //stu.score = 100
    //stu.age = 18
    //stu.id = 1001

    stu:=student4{person4{human{1001,"魏璎珞"},18,"女","皇宫"},3004,100}

    fmt.Println(stu)

}

/*

type 技能 struct{
    名称
    范围
    CD
    消耗
    伤害
    buff

}
type 人物信息 struct{

    姓名
    等级
    经验
    hp
    mp
    金钱

    skills []技能
}

 */

/*

type 消费记录 struct{

    时间
    金额
    地点
    流水号
    备注

}
type 信用卡 struct{

    卡号
    额度
    信誉度
    密码
    持卡人
    日期

    记录 []消费记录
}

 */

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~