-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAnimalExample.go
63 lines (49 loc) · 1.28 KB
/
AnimalExample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*Go实现多态的例子 */
package main
import (
"fmt"
)
// 定义一个Animal接口
type Animal interface {
MakeSound()
}
// 定义一个 Dog 类型
type Dog struct{}
// 实现 Animal 接口的 MakeSound 方法
func (d Dog) MakeSound() {
fmt.Println("Dog barks")
}
// 定义一个 Cat 类型
type Cat struct{}
// 实现 Animal 接口的 MakeSound 方法
func (c Cat) MakeSound() {
fmt.Println("Cat meows")
}
// Cat自有方法
func (c *Cat) Meow() {
fmt.Println("Cat is meowing...")
}
// 定义一个 Zoo 类型,用于管理动物
type Zoo struct{}
// 定义一个方法,让动物发出声音
func (z Zoo) LetAnimalMakeSound(a Animal) {
a.MakeSound()
}
type Bird struct{}
// 实现 Animal 接口的 MakeSound 方法
func (b Bird) MakeSound() {
fmt.Println("Bird chirps")
}
func main() {
zoo := Zoo{}
myDog := Dog{}
// 接口断言。用接口来声明结构体,类似父类声明子类
var myCat Animal = &Cat{}
// 类型断言。将接口转为子类类型,打印自有方法
(myCat.(*Cat)).Meow()
myBird := Bird{}
// 使用多态性,通过接口类型处理不同的具体类型
zoo.LetAnimalMakeSound(myDog) // 输出 "Dog barks"
zoo.LetAnimalMakeSound(myCat) // 输出 "Cat meows"
zoo.LetAnimalMakeSound(myBird) // 输出 "Bird chirps"
}