多态

  • 多态:同一件事情由于条件不同产生的结果不同

  • 由于Go语言中结构体不能相互转换,所以没有结构体(父子结构体)的多态,只有基于接口的多态.这也符合Go语言对面向对象的诠释

  • 多态在代码层面最常见的一种方式是接口当作方法参数

代码示例

  • 结构体实现了接口的全部方法,就认为结构体属于接口类型,这是可以把结构体变量赋值给接口变量

  • 重写接口时接收者为Type*Type的区别

    • *Type可以调用*TypeType作为接收者的方法.所以只要接口中多个方法中至少出现一个使用*Type作为接收者进行重写的方法,就必须把结构体指针赋值给接口变量,否则编译报错

    • Type只能调用Type作为接收者的方法

type Live interface {
    run()
    eat()
}
type People struct {
    name string
}

func (p *People) run() {
    fmt.Println(p.name, "正在跑步")
}
func (p People) eat() {
    fmt.Println(p.name, "在吃饭")
}

func main() {
    //重写接口时
    var run Live = &People{"张三"}
    run.run()
    run.eat()
}
  • 既然接口可以接收实现接口所有方法的结构体变量,接口也就可以作为方法(函数)参数
type Live interface {
    run()
}
type People struct{}
type Animate struct{}

func (p *People) run() {
    fmt.Println("人在跑")
}
func (a *Animate) run() {
    fmt.Println("动物在跑")
}

func sport(live Live) {
    fmt.Println(live.run)
}

func main() {
    peo := &People{}
    peo.run() //输出:人在跑
    ani := &Animate{}
    ani.run() //输出:动物在跑
}
原文地址:https://www.cnblogs.com/miaoweiye/p/12421708.html