14、组合与转发-GOLANG

合并结构

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
package main

import "fmt"

// type report struct {
// sol int
// high, low float64 //当我们需要更多的数据时这种写法显得不够灵活,这里的high和low也不好根据含义判断
// lat, long float64
// }
type temperature struct {
high, low float64 //在这里我们根据结构名一眼dingzhen,鉴定high,low为温度
}
type location struct {
lat, long float64
}
type report struct {
sol int
tem temperature //在结构中插入其他的结构类型,当小的结构类型需要调整时对大结构的影响相对较小
location location
}

func main() {
boadbury := location{-14.5684, 175.472636}
tem := temperature{high: -1.0, low: -78.0}
report := report{15, tem, boadbury}
fmt.Println(report)
}

为温度结构编辑方法实现计算平均温度

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
package main

import "fmt"

type temperature struct {
high, low celsius
}
type location struct {
lat, long float64
}
type report struct {
sol int
tem temperature
location location
}
type celsius float64

func (t temperature) average() celsius {
return (t.high + t.low) / 2
}
func main() {
//tem := temperature{high: -1.0, low: -78.0}
//fmt.Println("平均温度为", tem.average())//多种调用方式
boadbury := location{-14.5684, 175.472636}
tem := temperature{high: -1.0, low: -78.0}
report := report{15, tem, boadbury}

fmt.Println("平均温度为", report.tem.average())
}


为report类型编写直接访问平均温度的方法

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
package main

import "fmt"

type temperature struct {
high, low celsius
}
type location struct {
lat, long float64
}
type report struct {
sol int
tem temperature
location location
}
type celsius float64

func (t temperature) average() celsius {
return (t.high + t.low) / 2
}
func (r report) average() celsius {//同名函数,留个坑
return r.tem.average()
}
func main() {

boadbury := location{-14.5684, 175.472636}
tem1 := temperature{high: -1.0, low: -78.0}
report1 := report{15, tem1, boadbury}

fmt.Println("平均温度为", report1.average())
}

自动转发方法

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
package main

import "fmt"

type temperature struct {
high, low celsius
}
type location struct {
lat, long float64
}
type report struct {
sol int
temperature //不指定字段名,嵌入类型,go语言会将temperature类型所有方法自动为report所用
location
}
type celsius float64

func (t temperature) average() celsius {
return (t.high + t.low) / 2
}

func main() {

boadbury := location{-14.5684, 175.472636}
tem1 := temperature{high: -1.0, low: -78.0} //结构会自动为被嵌入的类型生成同名字段
report1 := report{15, tem1, boadbury}

fmt.Println("平均温度为", report1.average())
fmt.Println("最高温度为", report1.high) //还可以通过report类型的变量直接访问temperature的属性

}

命名冲突

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
package main

type sol int
type celsius float64
type temperature struct {
high, low celsius
}
type location struct {
lat, long float64
}
type report struct {
sol
temperature
location
}

func (s sol) days(s2 sol) int {
days := int(s2 - s)
if days < 0 {
days = -days
}
return days
}
func (l location) days(l2 location) int {
return 5
}
func main() {
boadbury := location{-14.5684, 175.472636}
tem1 := temperature{high: -1.0, low: -78.0}
report1 := report{15, tem1, boadbury}
d:=report1.days(1466)//在这个地方的调用会报错,解决方法在上面我们其实也用过,那就是给report也生命一个同名方法该方法优先级要高
}

ambiguous:有歧义的

image-20230311085723878

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
package main

import "fmt"

type sol int
type celsius float64
type temperature struct {
high, low celsius
}
type location struct {
lat, long float64
}
type report struct {
sol
temperature
location
}

func (s sol) days(s2 sol) int {
days := int(s2 - s)
if days < 0 {
days = -days
}
return days
}
func (l location) days(l2 location) int {
return 5
}
func (r report) days(s sol) int {//优先级最高的days方法,当我们需要另一个类型的days方法时只需要手动修改这里就行
return r.sol.days(s)
}
func main() {
boadbury := location{-14.5684, 175.472636}
tem1 := temperature{high: -1.0, low: -78.0}
report1 := report{15, tem1, boadbury}
d := report1.days(1466)
fmt.Println(d)
}