通过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
有疑问加站长微信联系(非本文作者)