• go 中的继承与多态


    继承

    package main
    
    import (
    	"fmt"
    )
    
    
    type Father struct {
    	name string
    	level int
    }
    
    func (*Father) Eat(){
    	fmt.Println("我是父类的 eat")
    }
    
    func (*Father) Food(){
    	fmt.Println("我是父类的 food")
    }
    
    type Son struct {
    	Father
    	work string
    }
    
    func (*Son) Eat(){
    	fmt.Println("我是子类的 eat")
    }
    
    func (*Son) Rice(){
    	fmt.Println("我是子类的 rice")
    }
    
    
    func main() {
    	f := Father{"张三", 1}
    	f.Eat() // 我是父类的 eat
    	f.Food() // 我是父类的 food
    
    	s := Son{f, "美食家"}
    	s.Eat() //我是子类的 eat
    	s.Rice() //我是子类的 rice
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    多态

    package main
    
    import (
    	"fmt"
    )
    
    // 定义一个动物的接口
    type Animal interface {
    	Sleep()
    	GetName() string
    	GetColor() string
    }
    
    // 定义一个结构体 猫
    type Cat struct {
    	name string
    }
    
    // 定义一个结构体 狗
    type Dog struct {
    	name string
    }
    
    
    func (*Dog) Sleep() {
    	fmt.Println("这只狗在睡觉。。。。。。")
    }
    
    func (this *Dog) GetName() string {
    	return this.name
    }
    
    func (this *Dog) GetColor() string {
    	return "黑色"
    }
    
    
    func (*Cat) Sleep() {
    	fmt.Println("这只猫在睡觉。。。。。。")
    }
    
    func (this *Cat) GetName() string {
    	return this.name
    }
    
    func (this *Cat) GetColor() string {
    	return "百色"
    }
    
    func ShowAnimal(animal Animal) {
    	animal.Sleep()
    	fmt.Println("color: ", animal.GetColor())
    	fmt.Println("name: ", animal.GetName())
    
    }
    
    func main() {
    	c := Cat{"小白"}
    	d := Dog{"小黑"}
    
    	ShowAnimal(&c)
    	ShowAnimal(&d)
    
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    例子

    package main
    
    import "fmt"
    
    
    type Builder interface {
    	Build()
    }
    
    type Director struct {
    	builder Builder
    }
    
    
    func (d *Director) Construct(){
    	d.builder.Build()
    }
    
    func NewDirector(builder Builder) Director {
    	return Director{builder:builder}
    }
    
    
    
    type ConcreteBuilder struct {
    	built bool
    }
    
    func (b *ConcreteBuilder) Build() {
    	fmt.Println("我被调用了。。。。")
    	b.built = true
    }
    
    func main() {
    	cb := ConcreteBuilder{false}
    	d := NewDirector(&cb)
    	d.Construct()
    
    	println("built is ", cb.built)
    
    
    	/*
    	我被调用了。。。。
    	true
    	*/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    【音视频】ffplay源码解析-FrameQueue队列
    Apache HTTPD 多后缀解析漏洞复现
    信号与系统 --- 卷积
    Scala012--Scala中的常用集合函数及操作Ⅲ
    golang指针和结构体、序列化
    Visual Studio 功能增强:CMake 目标视图
    22下半年:来长沙建第二支团队与所读的30本书(含哲学文学历史书单/笔记)
    CentOS 30分钟部署免费在线客服系统
    抖音微短剧小程序源码搭建:实现巨量广告数据高效回传
    算法--判断矩阵经轮转后是否一致
  • 原文地址:https://blog.csdn.net/qq_42874635/article/details/126607336