1、接口的继承

示例:

package main

import "fmt"

type Humaner interface { //子集
	sayhi()
}

type Personer interface { //超集
	Humaner //匿名字段,继承了sayhi()
	sing(lrc string)
}

type Student struct {
	name string
	id   int
}

//Student实现了sayhi()
func (tmp *Student) sayhi() {
	fmt.Printf("Student[%s, %d] sayhi\n", tmp.name, tmp.id)
}

func (tmp *Student) sing(lrc string) {
	fmt.Println("Student在唱着:", lrc)
}

func main() {
	//定义一个接口类型的变量
	var i Personer
	s := &Student{"mike", 666}
	i = s

	i.sayhi() //继承过来的方法
	i.sing("学生哥")
}

执行结果:

Student[mike, 666] sayhi
Student在唱着: 学生哥

  

 

相关文章:

  • 2022-12-23
  • 2021-08-14
  • 2021-10-29
  • 2021-09-16
  • 2021-08-24
  • 2021-12-31
  • 2022-01-11
  • 2022-01-30
猜你喜欢
  • 2021-10-22
  • 2021-06-21
  • 2021-12-05
  • 2021-10-04
  • 2022-01-04
  • 2022-02-22
  • 2021-12-17
相关资源
相似解决方案