Go-继承

一.继承的基本介绍和示意图

      1.继承可以解决代码复用,让我们的编程更加靠近人类思维。

      2.当多个结构体存在相同的属性(字段)和方法时,可以从这些结构体中抽象出结构体,在该结构体中定于这些相同的属性和方法

     3.其它的结构体不需要重新定义这些属性和方法,只需要嵌套一个Student匿名结构体即可。

     也就是说:在Golang中,如果一个struct嵌套了另一个匿名结构体,那么这个结构体可以直接访  问匿名结构体的字段和方法,从而实现继承特性

                          

    嵌套匿名结构体的基本语法:

                  

案例:

   没有继承前代码:

     

package main

import (
	"fmt"
)

type Pupil struct{
	Name string
	Age int
	Score int
}

func (p *Pupil) showInfo(){
	fmt.Printf("学生名=%v 年龄=%v 成绩=%v\n",p.Name,p.Age,p.Score)
}

func (p *Pupil) SetScore(score int){
	//业务判断
	p.Score = score
}

func (p *Pupil) testing(){
	//业务判断
	fmt.Println("小学生正在考试中.....")
}

//大学生
type Graduate struct{
	Name string
	Age int
	Score int
}

func (g *Graduate) showInfo(){
	fmt.Printf("学生名=%v 年龄=%v 成绩=%v\n",g.Name,g.Age,g.Score)
}

func (g *Graduate) SetScore(score int){
	//业务判断
	g.Score = score
}

func (g *Graduate) testing(){
	//业务判断
	fmt.Println("大学生正在考试中.....")
}

func main(){
	//测试
	var pupil = &Pupil{
		Name : "Tom",
		Age : 10,
	}
	pupil.testing()
	pupil.SetScore(90)
	pupil.showInfo()
	
    //测试
	var graduate = &Graduate{
		Name : "Bob",
		Age : 20,
	}
	graduate.testing()
	graduate.SetScore(90)
	graduate.showInfo()
}

  使用继承方法后的代码:

package main

import(
	"fmt"
)

type Student struct{
	Name string
	Age int
	Score int
}

//将pupil 和 Grduate 共有的方法也绑定到 *Student

type Pupil struct{
	Student  //嵌入了Student匿名结构体
}

type Graduate struct{
	Student  //嵌入了Student匿名结构体
}

func (stu *Student) showInfo(){
	fmt.Printf("学生名=%v 年龄=%v 成绩=%v\n",stu.Name,stu.Age,stu.Score)
}

func (stu *Student) SetScore(score int){
	stu.Score = score
}

//这时pupil结构体特有的方法,保留
func (p *Pupil) testing(){
	//业务判断
	fmt.Println("小学生正在考试中.....")
}
//这时Grduate结构体特有的方法,保留
func (g *Graduate) testing(){
	//业务判断
	fmt.Println("大学生正在考试中.....")
}

//给 *Student 增加一个方法,那么Pupil 和 Graduate都可以使用该方法
func (stu *Student) GetSum(n1 int,n2 int) int{
	return n1 + n2
}

func main(){
	//当我们对结构体嵌入了匿名结构体使用方法会发生变化
	pupil := &Pupil{}
	pupil.Student.Name = "Tom"
	pupil.Student.Age = 8
	pupil.testing()
	pupil.Student.SetScore(70)
	pupil.Student.showInfo()
	fmt.Println("res=",pupil.Student.GetSum(1,2))

	graduate := &Graduate{}
	graduate.Student.Name = "jerry"
	graduate.Student.Age = 20
	graduate.testing()
	graduate.Student.SetScore(90)
	graduate.Student.showInfo()
	fmt.Println("res=",graduate.Student.GetSum(4,5))
}

继承的深入了解

  1.结构体可以使用嵌套匿名结构体所有的字段和方法,即:首字母大写或者小写的字段、方法、都可以使用。

  2.匿名结构体字段访问可以简化:

         简化--->     

   3.当结构体和匿名结构体有相同的字段或者方法时,编译器采用就近访问原则访问,如希望访问匿名结构体的字段和方法,可以通过匿名结构体名来区分                    

package main

import(
	"fmt"
)

type A struct{
	Name string
	age int
}

func (a *A) SayOk(){
	fmt.Println("A SayOk",a.Name)
}

func (a *A) hello(){
	fmt.Println("A hello",a.Name)
}

type B struct{
	A
	Name string
}

func (b *B) SayOk(){
	fmt.Println("B sayOK",b.Name)
}

func main(){
	var b B
	b.Name = "jack" //这时就近原则,会访问B结构体的name字段
	//b.A.Name 就明确指定访问A匿名结构体的字段name
	b.A.Name = "tom"
	b.age = 78
	b.SayOk()//这时就近原则,会访问B结构体的say函数
	b.hello()
	//b.A.hello() 就明确指定访问A匿名结构体的方法hello()
	b.A.hello()

}

    4.结构体嵌入两个(或多个)匿名结构体,如两个匿名结构体有相同的字段和方法(同时结构体本身没有同名的字段和方法),在访问时,就必须明确指定匿名结构体名字,否则编译报错。

                         

  5.如果一个struct嵌套了一个有名结构体,这种模式就是组合,如果是组合关系,那么在访问组合的结构体或方法时,必须带上结构体的名字。

                             

  6.嵌套匿名结构体后,也可以在创建结构体变量(实例时),直接指定各个匿名结构体字段的值  

package main

import (
	"fmt"
)

type Goods struct{
	Name string
	Price float64
}

type Brand struct{
	Name string
	Address string
}

type TV struct{
	Goods
	Brand
}

type TV2 struct{
	*Goods
	*Brand
}

func main(){
	//方式一:
	tv := TV{ Goods{"电视机001",5000},Brand{"海尔","武汉"},}
	fmt.Println("tv",tv)

	//方式二:
	tv2 := TV{
		Goods{
			Price : 4000,
			Name : "电视机003",
		},
		Brand{
			Name : "长虹",
			Address : "武汉",
		},
	}
	fmt.Println("tv2",tv2)

    //方式三
	tv3 := TV2{&Goods{"洗衣机",3000},&Brand{"小天鹅","武汉"},}
	fmt.Println("tv3",*tv3.Goods,*tv3.Brand)
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老实憨厚的腾锅锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值