package main
import (
"fmt"
)
type Animals interface {
Dog() string
Cat() string
}
type My struct {
}
func (this My) Dog() string {
return "Dog"
}
func (this My) Cat() string {
return "Cat"
}
func printDog(animals Animals) {
fmt.Println(animals.Dog())
}
func printCat(animals Animals) {
fmt.Println(animals.Cat())
}
func main() {
var my My
//My结构体实现 Animals
printDog(my)
printCat(my)
}
执行结果
Dog
Cat