当前位置 博文首页 > 莫忘输赢的博客:Go-一个父类调用子类方法的例子
package main
import "fmt"
type MyInterface interface {
PrintSelf()
}
type MyInstance struct {
MyInterface
}
func (ins *MyInstance) PrintSelf() {
fmt.Println("i am inst")
}
func (ins *MyInstance) PrintSpecial() {
fmt.Println("i am special")
}
//func MyFun(myInter MyInterface) {
// myInter.(interface{ printSpecial() }).printSpecial()
//}
func main() {
var Inf MyInterface = &MyInstance{}
//
Inf.PrintSelf()
//父类调用子类方法
Inf.(interface{ PrintSpecial() }).PrintSpecial()
}