1. 程式人生 > >閒來無聊,養狗養貓養烏龜

閒來無聊,養狗養貓養烏龜

       家裡養過狗,養過貓,也養過龜, 來看看:

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))
}

         不多說。