G
G
Go语言圣经
Search…
G
G
Go语言圣经
前言
Go语言起源
Go语言项目
本书的组织
更多的信息
致谢
入门
程序结构
基础数据类型
复合数据类型
函数
方法
方法声明
基于指针对象的方法
通过嵌入结构体来扩展类型
方法值和方法表达式
示例: Bit数组
封装
接口
Goroutines和Channels
基于共享变量的并发
包和工具
测试
反射
底层编程
附录
Powered By
GitBook
通过嵌入结构体来扩展类型
来看看ColoredPoint这个类型:
gopl.io/ch6/coloredpoint
1
import
"image/color"
2
3
type
Point
struct
{
X
,
Y
float64
}
4
5
type
ColoredPoint
struct
{
6
Point
7
Color color
.
RGBA
8
}
Copied!
我们完全可以将ColoredPoint定义为一个有三个字段的struct,但是我们却将Point这个类型嵌入到ColoredPoint来提供X和Y这两个字段。像我们在4.4节中看到的那样,内嵌可以使我们在定义ColoredPoint时得到一种句法上的简写形式,并使其包含Point类型所具有的一切字段,然后再定义一些自己的。如果我们想要的话,我们可以直接认为通过嵌入的字段就是ColoredPoint自身的字段,而完全不需要在调用时指出Point,比如下面这样。
1
var
cp ColoredPoint
2
cp
.
X
=
1
3
fmt
.
Println
(
cp
.
Point
.
X
)
// "1"
4
cp
.
Point
.
Y
=
2
5
fmt
.
Println
(
cp
.
Y
)
// "2"
Copied!
对于Point中的方法我们也有类似的用法,我们可以把ColoredPoint类型当作接收器来调用Point里的方法,即使ColoredPoint里没有声明这些方法:
1
red
:=
color
.
RGBA
{
255
,
0
,
0
,
255
}
2
blue
:=
color
.
RGBA
{
0
,
0
,
255
,
255
}
3
var
p
=
ColoredPoint
{
Point
{
1
,
1
},
red
}
4
var
q
=
ColoredPoint
{
Point
{
5
,
4
},
blue
}
5
fmt
.
Println
(
p
.
Distance
(
q
.
Point
))
// "5"
6
p
.
ScaleBy
(
2
)
7
q
.
ScaleBy
(
2
)
8
fmt
.
Println
(
p
.
Distance
(
q
.
Point
))
// "10"
Copied!
Point类的方法也被引入了ColoredPoint。用这种方式,内嵌可以使我们定义字段特别多的复杂类型,我们可以将字段先按小类型分组,然后定义小类型的方法,之后再把它们组合起来。
读者如果对基于类来实现面向对象的语言比较熟悉的话,可能会倾向于将Point看作一个基类,而ColoredPoint看作其子类或者继承类,或者将ColoredPoint看作"is a" Point类型。但这是错误的理解。请注意上面例子中对Distance方法的调用。Distance有一个参数是Point类型,但q并不是一个Point类,所以尽管q有着Point这个内嵌类型,我们也必须要显式地选择它。尝试直接传q的话你会看到下面这样的错误:
1
p
.
Distance
(
q
)
// compile error: cannot use q (ColoredPoint) as Point
Copied!
一个ColoredPoint并不是一个Point,但他"has a"Point,并且它有从Point类里引入的Distance和ScaleBy方法。如果你喜欢从实现的角度来考虑问题,内嵌字段会指导编译器去生成额外的包装方法来委托已经声明好的方法,和下面的形式是等价的:
1
func
(
p ColoredPoint
)
Distance
(
q Point
)
float64
{
2
return
p
.
Point
.
Distance
(
q
)
3
}
4
5
func
(
p
*
ColoredPoint
)
ScaleBy
(
factor
float64
)
{
6
p
.
Point
.
ScaleBy
(
factor
)
7
}
Copied!
当Point.Distance被第一个包装方法调用时,它的接收器值是p.Point,而不是p,当然了,在Point类的方法里,你是访问不到ColoredPoint的任何字段的。
在类型中内嵌的匿名字段也可能是一个命名类型的指针,这种情况下字段和方法会被间接地引入到当前的类型中(译注:访问需要通过该指针指向的对象去取)。添加这一层间接关系让我们可以共享通用的结构并动态地改变对象之间的关系。下面这个ColoredPoint的声明内嵌了一个*Point的指针。
1
type
ColoredPoint
struct
{
2
*
Point
3
Color color
.
RGBA
4
}
5
6
p
:=
ColoredPoint
{
&
Point
{
1
,
1
},
red
}
7
q
:=
ColoredPoint
{
&
Point
{
5
,
4
},
blue
}
8
fmt
.
Println
(
p
.
Distance
(
*
q
.
Point
))
// "5"
9
q
.
Point
=
p
.
Point
// p and q now share the same Point
10
p
.
ScaleBy
(
2
)
11
fmt
.
Println
(
*
p
.
Point
,
*
q
.
Point
)
// "{2 2} {2 2}"
Copied!
一个struct类型也可能会有多个匿名字段。我们将ColoredPoint定义为下面这样:
1
type
ColoredPoint
struct
{
2
Point
3
color
.
RGBA
4
}
Copied!
然后这种类型的值便会拥有Point和RGBA类型的所有方法,以及直接定义在ColoredPoint中的方法。当编译器解析一个选择器到方法时,比如p.ScaleBy,它会首先去找直接定义在这个类型里的ScaleBy方法,然后找被ColoredPoint的内嵌字段们引入的方法,然后去找Point和RGBA的内嵌字段引入的方法,然后一直递归向下找。如果选择器有二义性的话编译器会报错,比如你在同一级里有两个同名的方法。
方法只能在命名类型(像Point)或者指向类型的指针上定义,但是多亏了内嵌,有些时候我们给匿名struct类型来定义方法也有了手段。
下面是一个小trick。这个例子展示了简单的cache,其使用两个包级别的变量来实现,一个mutex互斥量(§9.2)和它所操作的cache:
1
var
(
2
mu sync
.
Mutex
// guards mapping
3
mapping
=
make
(
map
[
string
]
string
)
4
)
5
6
func
Lookup
(
key
string
)
string
{
7
mu
.
Lock
()
8
v
:=
mapping
[
key
]
9
mu
.
Unlock
()
10
return
v
11
}
Copied!
下面这个版本在功能上是一致的,但将两个包级吧的变量放在了cache这个struct一组内:
1
var
cache
=
struct
{
2
sync
.
Mutex
3
mapping
map
[
string
]
string
4
}{
5
mapping
:
make
(
map
[
string
]
string
),
6
}
7
8
9
func
Lookup
(
key
string
)
string
{
10
cache
.
Lock
()
11
v
:=
cache
.
mapping
[
key
]
12
cache
.
Unlock
()
13
return
v
14
}
Copied!
我们给新的变量起了一个更具表达性的名字:cache。因为sync.Mutex字段也被嵌入到了这个struct里,其Lock和Unlock方法也就都被引入到了这个匿名结构中了,这让我们能够以一个简单明了的语法来对其进行加锁解锁操作。
Previous
基于指针对象的方法
Next
方法值和方法表达式
Last modified
2yr ago
Copy link