继承与接口同时存在
-
在Golang语言中,可以这么说:接口是继承的功能补充!
-
武当派有一个徒弟结构体,它继承WuDangMaster结构体的字段及方法
-
武林之中还有一个泰山北斗,名约少林派,少林入门神功伏虎罗汉拳
-
武当徒弟张翠山拥有武当太极十三式,但是也想学会少林的伏虎罗汉拳,这该怎么办?
package main import( "fmt" _"sort" _"math/rand" ) // type WuDangMaster struct { Name string Age int } type ShaoLinMaster interface { skill() } func (master *WuDangMaster) kongfu() { fmt.Println(master.Name,"秘籍:武当太极十三式") } //apprentice 美 /ə'prɛntɪs/ n. 学徒;生手 type apprentice struct{ WuDangMaster //匿名结构体,继承WuDangMaster结构体内的字段(属性)及方法 } func (master *apprentice) skill(){ fmt.Println(master.Name,"终于学会了少林秘籍:伏虎罗汉拳") } func main(){ var appr apprentice = apprentice{ WuDangMaster{ Name:"张翠山", Age:30, }, } appr.kongfu() appr.skill() }