当前位置 博文首页 > 莫忘输赢的博客:Go-一个父类调用子类方法的例子

    莫忘输赢的博客:Go-一个父类调用子类方法的例子

    作者:[db:作者] 时间:2021-07-25 12:17

    1、代码

    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()
    }
    

    2、输出

    cs
    下一篇:没有了