forked from FAuditore/golang-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP.go
69 lines (57 loc) · 1 KB
/
OOP.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
64
65
66
67
68
69
package main
import "fmt"
func main() {
/*
结构体内匿名结构体的属性是该结构体的提升字段,可以直接访问
重复的字段,会覆盖匿名结构体
模拟继承
type A struct{
field
}
type B struct{
A //匿名结构体
field
}
模拟聚合关系 has a
type A struct{
field
}
type B struct{
a A
field
}
*/
var student S
student.P.name = "pname"
student.name = "sname" //student.P.name="s" 提升字段
student.age = 15
student.grade = "2017"
fmt.Println(student)
c1 := cat{}
c1.name = "cat"
c1.like = "fish"
c1.eat() //调用子类重写的父类方法 不重写默认调用父类的
}
type P struct {
name string
age int
}
//P中的字段对于S来说就是提升字段,可以直接通过S进行访问
type S struct {
P
grade string
name string
}
type animal struct {
name string
}
type cat struct {
animal
like string
}
func (a animal) eat() {
fmt.Println(a, "eat")
}
func (c cat) eat() {
fmt.Println(c, "eat fish")
}