目录
个人资料
结构体
声明/定义
实现和使用
实现
多接口的实现
接口继承
空接口
结构切片排序
接口和继承的比较
注意事项
所有代码
截图
参考资料
简介Go语言的接口是嵌入式的,定义了一组方法的签名,体现了编程3358 www.Sina.com /的特点。 本文介绍界面和基本使用方法,下一篇文章介绍类型断言。
结构体定义Monkey结构体,具有climb方法。
typemonkeystruct { name string } func (m * monkey ) climb ) ) fmt.println ) m.name,'可以爬树.')定义WuKong结构为monkon
typewukongstruct { monkeytoolstring }声明/定义界面模板如下:
/*接口定义*
type接口_ name接口{
method _ name1([参数列表return_type ) )。
method _ name2([参数列表return_type ) ) ) )。
.
method _ namen (([参数列表return_type ) ) ) ) ) ) ) )。
}
高内聚低耦合
定义Flyable接口
typeflyableinterface{Fly(}实现和使用实现代码实现了此接口,并显示了它是哪个接口
func(w*Wukong ) Fly ) ({fmt.Println ) )筋斗云,来.) } 实现了接口的所有方法
3358 www.Sina.com/(http://www.Sina.com/)是一种动态型式,例如在Python语言中很常见。 在此样式中,对象的有效语义由'隐式实现'决定,而不是从特定类继承或实现特定接口。
另一方面,在Go中,鸭子类型决定了当前的结构体鸭子类型。 WuKong会飞,但Monkey不会飞。 WuKong在继承Monkey的基础上扩展了自己的功能。
再接一点
口实现再定义一个接口
type Swimmable interface {Swim()}实现
func (w *WuKong) Swim() {fmt.Println("老龙王,俺危机的啤酒的金箍棒呢?")}WuKong结构体就实现了上面的两个接口。
接口继承再定义一个接口Skills,继承前面的两个接口,并添加新的方法Change
type Skills interface {FlyableSwimmableChange()}实现
func (w *WuKong) Change() {fmt.Println("看我72变...")}至此, WuKong结构体实现了四个接口,为什么是四个呢?因为还有下面这一个
空接口 type Null interface {}如你所见,什么方法也没有,即每个结构体都实现了空接口,都可以赋值给空接口,这在排序,后面类型断言等部分都很有用。
结构体切片排序func Sort(data Interface)
Sort,本地排序data,无返回。它调用1次data.Len确定长度,调用O(n*log(n))次data.Less和data.Swap,底层使用的是快排,感兴趣的朋友可以看看快排源代码。本函数不能保证排序的稳定性(即不保证相等元素的相对次序不变)。
// Sort sorts data.// It makes one call to data.Len to determine n and O(n*log(n)) calls to// data.Less and data.Swap. The sort is not guaranteed to be stable.func Sort(data Interface) {n := data.Len()quickSort(data, 0, n, maxDepth(n))}Interface接口
type Interface interface {// Len is the number of elements in the collection.Len() int // Less reports whether the element with index i must sort before the element with index j.// See Float64Slice.Less for a correct implementation for floating-point values.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)}既然这是一个接口,只要我们实现了Len、Less、Swap方法即可传参,进行排序。
我们就用Monkey结构体的切片实现一下这个接口。
type MonkeySlice []Monkey//--------实现Len--------func (m MonkeySlice) Len() int{return len(m)}//---------实现Less---------func (m MonkeySlice) Less(i,j int) bool {return m[i].Name < m[j].Name}//----------实现Swap---------func (m MonkeySlice) Swap(i,j int){m[i],m[j] = m[j],m[i]}使用
monkeys := MonkeySlice{{"峨眉猴"},{"大胆的八宝粥"},{"金丝猴"},{"六耳猕猴"},{"清秀的月饼"}}sort.Sort(monkeys)fmt.Println(monkeys) 接口和继承比较 当A结构体继承了B结构体,那么A结构体就自动的继承了B结构体的字段和方法,并且可以直接使用当A结构体需要扩展功能,同时不希望去破坏继承关系,则可以去实现某个接口即可,接口是对继承的补充,是like和is的区别继承解决代码的复用性和可维护性接口更加灵活,一定程度上实现了解耦注意项 接口不能创建实例,接口可以指向实现了接口的结构体实例结构体必须实现所有接口的方法才可以说实现了该接口接口默认是指针类型,没有初始化会输出nil空接口可以接收任何类型全部代码 package mainimport ("fmt""sort")type Monkey struct {Name string}func (m *Monkey)climb(){fmt.Println(m.Name,"会爬树...")}//--------Flyable接口---------type Flyable interface {Fly()}//--------Swimmable接口-------type Swimmable interface {Swim()}//----------接口继承--------------type Skills interface {FlyableSwimmableChange()}//-----------空接口-----------type Null interface {}//---------不破坏结构体,添加属性--------type WuKong struct {MonkeyTool string}//---------不破坏继承,扩展行为/方法--------func (w *WuKong) Fly() {fmt.Println("筋斗云,来...")}func (w *WuKong) Swim() {fmt.Println("老龙王,俺危机的啤酒的金箍棒呢?")}func (w *WuKong) Change() {fmt.Println("看我72变...")}type MonkeySlice []Monkey//--------实现Len--------func (m MonkeySlice) Len() int{return len(m)}//---------实现Less---------func (m MonkeySlice) Less(i,j int) bool {return m[i].Name < m[j].Name}//----------实现Swap---------func (m MonkeySlice) Swap(i,j int){m[i],m[j] = m[j],m[i]}func main() {//-----------接口体-------w := WuKong{}w.Name = "清秀的月饼"//-----------使用方法------w.climb()//-----------使用实现的接口的方法---------w.Fly()//-----------结构体赋值给接口------------var s Skillss = &ws.Change()//----------空接口-------------var null Nullnull = wwukong := null.(WuKong)wukong.Swim()//-----------结构体切片排序--------------monkeys := MonkeySlice{{"峨眉猴"},{"大胆的八宝粥"},{"金丝猴"},{"六耳猕猴"},{"清秀的月饼"}}sort.Sort(monkeys)fmt.Println(monkeys)} 截图 参考Go标准库-sort
更多Go相关内容:Go-Golang学习总结笔记