forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
648 B
43 lines
648 B
package main
|
|
|
|
import "github.com/golang/glog"
|
|
|
|
// Animal 类型是一个接口,我们将定义一个 Animal 作为任何可以说话的东西
|
|
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!"
|
|
}
|
|
|
|
type Llama struct {
|
|
}
|
|
|
|
func (l Llama) Speak() string {
|
|
return "?????"
|
|
}
|
|
|
|
type JavaProgrammer struct {
|
|
}
|
|
|
|
func (j JavaProgrammer) Speak() string {
|
|
return "Design patterns!"
|
|
}
|
|
|
|
func main() {
|
|
animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}}
|
|
for _, animal := range animals {
|
|
glog.Info(animal.Speak())
|
|
}
|
|
}
|
|
|