-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.go
60 lines (43 loc) · 1.36 KB
/
main.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
package main
import (
"fmt"
)
type Monkey struct {
Name string
}
type BirdAble interface {
Flying()
}
type Fishable interface {
Swimming()
}
type LittleMonkey struct {
Monkey //继承
}
func (this *Monkey) climbing() {
fmt.Println(this.Name, "生来会爬树")
}
func (this *LittleMonkey) Flying() {
fmt.Println(this.Name, "通过学习,我会飞翔了")
}
func (this *LittleMonkey) Swimming() {
fmt.Println(this.Name, "通过学习,我会游泳了")
}
func main() {
monkey := LittleMonkey{
Monkey{
Name: "悟空",
},
}
monkey.climbing()
monkey.Flying()
monkey.Swimming()
}
//当 A 结构体继承了 B 结构体,那么 A 结构就自动的继承了 B 结构体的字段和方法,并且可以直接使用
//当 A 结构体需要扩展功能,同时不希望去破坏继承关系,则可以去实现某个接口即可,因此我们可以认为:实现接口是对继承机制的补充.
//接口和继承解决的解决的问题不同
//继承的价值主要在于:解决代码的复用性和可维护性。
//接口的价值主要在于:设计,设计好各种规范(方法),让其它自定义类型去实现这些方法。
//接口比继承更加灵活 Person Student BirdAble LittleMonkey
//接口比继承更加灵活,继承是满足 is - a 的关系,而接口只需满足 like - a 的关系。
//接口在一定程度上实现代码解耦