go多态
时间:2021-09-15 20:42:25
收藏:0
阅读:23
go多态
package main
import (
"fmt"
)
type Animal interface {
Speak() string
}
type Dog struct {
}
func (d Dog) Speak() string {
return "Woof!"
}
type Cat struct {
}
func (c Cat) Speak() string {
return "Meow!"
}
func main() {
animals := []Animal{Dog{}, Cat{}}
for _, animal := range animals {
fmt.Println(animal.Speak())
}
}
原文:https://www.cnblogs.com/hnxxcxg/p/15265863.html
评论(0)