例子
package main
import "fmt"
//定义一个 anial 接口类型
type anial interface {
move()
}
type cat struct {
name string
feet int
}
type dog struct {
name string
feet int
}
// 采用值接收者,参数可以传递结构体类型和结构体指针类型
//func (c cat)move() {
// fmt.Printf("%s,走猫步\n", c.name)
//}
//
//func (d dog)move() {
// fmt.Printf("%s,走狗步\n", d.name)
//}
// 采用指针接收者,参数仅可以传递结构体指针类型
func (c cat)move() {
fmt.Printf("%s,走猫步\n", c.name)
}
func (d dog)move() {
fmt.Printf("%s,走狗步\n", d.name)
}
func main() {
var a anial
c1 := cat{
name:"Tom",
feet:4,
}
a = &c1
a.move()
a = &dog{
name:"wawa",
feet:4,
}
a.move()
}