struct 的匿名成员

2017-08-17  本文已影响0人  孙小兵

package main

import"fmt"

type Animal interface{

eat()

}

type Live interface{

work()

}

type Dog struct{

name string

}

type People struct{

age int

}

func(dogDog)eat() {

fmt.Println(dog.name)

}

func(pPeople)work() {

fmt.Println(p.age)

}

type Family struct{

Dog

People

}

func main() {

f := Family{}

f.eat()

f.work()

}

Family 自动拥有 Dog , People 的所有方法 

上一篇下一篇

猜你喜欢

热点阅读