mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:06:41 +08:00
@@ -16,7 +16,7 @@ func Sort(data Sorter) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`Sort` 函数接收一个接口类型参数:`Sorter` ,它声明了这些方法:
|
`Sort` 函数接收一个接口类型的参数:`Sorter` ,它声明了这些方法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Sorter interface {
|
type Sorter interface {
|
||||||
@@ -47,7 +47,7 @@ sort.Sort(a)
|
|||||||
|
|
||||||
完整的、可运行的代码可以在 `sort.go` 和 `sortmain.go` 里找到。
|
完整的、可运行的代码可以在 `sort.go` 和 `sortmain.go` 里找到。
|
||||||
|
|
||||||
同样的原理,排序函数可以用于一个浮点型数组,一个字符串数组,或者一个表示每周各天的结构体 `dayArray`.
|
同样的原理,排序函数可以用于一个浮点型数组,一个字符串数组,或者一个表示每周各天的结构体 `dayArray`。
|
||||||
|
|
||||||
示例 11.6 [sort.go](examples/chapter_11/sort.go):
|
示例 11.6 [sort.go](examples/chapter_11/sort.go):
|
||||||
|
|
||||||
@@ -192,7 +192,7 @@ type Interface interface {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
这个接口总结了需要用于排序的抽象方法,函数 `Sort(data Interface)` 用来对此类对象进行排序,可以用它们来实现对其他数据(非基本类型)进行排序。在上面的例子中,我们也是这么做的,不仅可以对 `int` 和 `string` 序列进行排序,也可以对用户自定义类型 `dayArray` 进行排序。
|
这个接口总结了需要用于排序的抽象方法,函数 `Sort(data Interface)` 用来对此类对象进行排序,可以用它们来实现对其他类型的数据(非基本类型)进行排序。在上面的例子中,我们也是这么做的,不仅可以对 `int` 和 `string` 序列进行排序,也可以对用户自定义类型 `dayArray` 进行排序。
|
||||||
|
|
||||||
**练习 11.5** interfaces_ext.go:
|
**练习 11.5** interfaces_ext.go:
|
||||||
|
|
||||||
@@ -224,4 +224,4 @@ b). 定义一个新接口 `PeriInterface`,它有一个 `Perimeter` 方法。
|
|||||||
|
|
||||||
- [目录](directory.md)
|
- [目录](directory.md)
|
||||||
- 上一节:[使用方法集与接口](11.6.md)
|
- 上一节:[使用方法集与接口](11.6.md)
|
||||||
- 下一节:[第二个例子:读和写](11.8.md)
|
- 下一节:[第二个例子:读和写](11.8.md)
|
||||||
|
Reference in New Issue
Block a user