Golang learning 面向对象 多态
2019-05-23 本文已影响0人
wangyongyue
通过interface 实现多态
type Cat struct {
Animal
teeth string "牙"
leg int
}
type Dog struct {
Animal
teeth string "牙"
leg int
}
type NewAction interface {
run()
}
func (c Cat) run(){
fmt.Print("\ncat----run")
}
func (c Dog) run(){
fmt.Print("\nDog----run")
}
func main() {
cat := Cat{}
dog := Dog{}
animalRun(cat)
animalRun(dog)
}
打印
cat----run
Dog----run