struct interface 就可以实现面向对象中的继承,封装,多态
继承的演示:
tsh类型继承people类型,并且使用people类型的方法
多态的演示
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() }
如对本文有疑问, 点击进行留言回复!!
VSCode1.4 搭建Golang的开发调试环境(遇到很多问题)
网友评论