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() }
原文:https://www.cnblogs.com/taoshihan/p/11945876.html