4.8 go 接口的继承

package main

import "fmt"

type Humaner interface { //超级接口
	//只声明,有别的类型实现
	SayHi()
	Actioner
}
type Actioner interface { //子接口
	PlayBall(ball string)
}

type Student struct {
	name string
	age  int
}

func (s *Student) SayHi() {
	fmt.Println("此学生叫", s.name)

}
func (*Student) PlayBall(ball string) {
	fmt.Println("在玩", ball)

}

func main() {
	//定义接口变量
	var i Humaner
	s := Student{"小王", 22}
	i = &s
	i.SayHi()
	//01接口的继承
	i.PlayBall("篮球")
	//02接口的转换  父类可以转化为子类,不在具有父类特点
	var act Actioner
	act = i
	act.PlayBall("乒乓球")

}
发布了124 篇原创文章 · 获赞 94 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/h4241778/article/details/105318179
4.8