版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84665357
家里养过狗,养过猫,也养过龟, 来看看:
package main
import "fmt"
type Animal interface {
move()
}
type Dog struct {}
func (p *Dog) move() {
fmt.Println("dog move")
}
type Cat struct {}
func (p *Cat) move() {
fmt.Println("cat move")
}
type Turtle struct {}
func (p *Turtle) move() {
fmt.Println("turtle move")
}
func Run(intf Animal) {
intf.move()
}
func main() {
Run(new(Dog))
Run(new(Cat))
Run(new(Turtle))
}
不多说。