struct interface 就可以实现面向对象中的继承,封装,多态

继承的演示:
Tsh类型继承People类型,并且使用People类型的方法

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

多态的演示
Tsh类型实现了接口Student,实现了接口定义的方法

完整代码:

package main

import "fmt"

//父类型
type People struct {
}

func (p *People) echo() {
    fmt.Println("taoshihan")
}

//接口
type Student interface {
    Do()
}

//子类型,实现了接口,继承了父类型
type Tsh struct {
    People
}

func (t Tsh) Do() {
    fmt.Println("taoshihan do")
}
func main() {
    //继承的演示
    t := Tsh{People{}}
    t.echo()
    //多态的演示
    var student Student
    student = t
    student.Do()
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄