Files
the-way-to-go_ZH_CN/eBook/10.5.md
Haigang Zhou d3979794f4 第十章修改 (#840)
Co-authored-by: Joe Chen <jc@unknwon.io>
2022-05-11 22:58:19 +08:00

130 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 10.5 匿名字段和内嵌结构体
## 10.5.1 定义
结构体可以包含一个或多个 **匿名(或内嵌)字段**,即这些字段没有显式的名字,只有字段的类型是必须的,此时类型就是字段的名字。匿名字段本身可以是一个结构体类型,即 **结构体可以包含内嵌结构体**
可以粗略地将这个和面向对象语言中的继承概念相比较随后将会看到它被用来模拟类似继承的行为。Go 语言中的继承是通过内嵌或组合来实现的,所以可以说,在 Go 语言中,相比较于继承,组合更受青睐。
考虑如下的程序:
示例 10.8 [structs_anonymous_fields.go](examples/chapter_10/structs_anonymous_fields.go)
```go
package main
import "fmt"
type innerS struct {
in1 int
in2 int
}
type outerS struct {
b int
c float32
int // anonymous field
innerS //anonymous field
}
func main() {
outer := new(outerS)
outer.b = 6
outer.c = 7.5
outer.int = 60
outer.in1 = 5
outer.in2 = 10
fmt.Printf("outer.b is: %d\n", outer.b)
fmt.Printf("outer.c is: %f\n", outer.c)
fmt.Printf("outer.int is: %d\n", outer.int)
fmt.Printf("outer.in1 is: %d\n", outer.in1)
fmt.Printf("outer.in2 is: %d\n", outer.in2)
// 使用结构体字面量
outer2 := outerS{6, 7.5, 60, innerS{5, 10}}
fmt.Println("outer2 is:", outer2)
}
```
输出:
outer.b is: 6
outer.c is: 7.500000
outer.int is: 60
outer.in1 is: 5
outer.in2 is: 10
outer2 is:{6 7.5 60 {5 10}}
通过类型 `outer.int` 的名字来获取存储在匿名字段中的数据,于是可以得出一个结论:在一个结构体中对于每一种数据类型只能有一个匿名字段。
## 10.5.2 内嵌结构体
同样地结构体也是一种数据类型,所以它也可以作为一个匿名字段来使用,如同上面例子中那样。外层结构体通过 `outer.in1` 直接进入内层结构体的字段,内嵌结构体甚至可以来自其他包。内层结构体被简单的插入或者内嵌进外层结构体。这个简单的“继承”机制提供了一种方式,使得可以从另外一个或一些类型继承部分或全部实现。
另外一个例子:
示例 10.9 [embedd_struct.go](examples/chapter_10/embedd_struct.go)
```go
package main
import "fmt"
type A struct {
ax, ay int
}
type B struct {
A
bx, by float32
}
func main() {
b := B{A{1, 2}, 3.0, 4.0}
fmt.Println(b.ax, b.ay, b.bx, b.by)
fmt.Println(b.A)
}
```
输出:
1 2 3 4
{1 2}
**练习 10.5** [anonymous_struct.go](exercises\chapter_10\anonymous_struct.go)
创建一个结构体,它有一个具名的 `float32` 字段2 个匿名字段,类型分别是 `int``string`。通过结构体字面量新建一个结构体实例并打印它的内容。
## 10.5.3 命名冲突
当两个字段拥有相同的名字(可能是继承来的名字)时该怎么办呢?
1. 外层名字会覆盖内层名字(但是两者的内存空间都保留),这提供了一种重载字段或方法的方式;
2. 如果相同的名字在同一级别出现了两次,如果这个名字被程序使用了,将会引发一个错误(不使用没关系)。没有办法来解决这种问题引起的二义性,必须由程序员自己修正。
例子:
```go
type A struct {a int}
type B struct {a, b int}
type C struct {A; B}
var c C
```
规则 2使用 `c.a` 是错误的,到底是 `c.A.a` 还是 `c.B.a` 呢?会导致编译器错误:**`ambiguous DOT reference c.a disambiguate with either c.A.a or c.B.a`**。
```go
type D struct {B; b float32}
var d D
```
规则1使用 `d.b` 是没问题的:它是 `float32`,而不是 `B``b`。如果想要内层的 `b` 可以通过 `d.B.b` 得到。
## 链接
- [目录](directory.md)
- 上一节:[带标签的结构体](10.4.md)
- 下一节:[方法](10.6.md)