Hi,你们好,我是明哥。git
在本身学习 Golang 的这段时间里,我写了详细的学习笔记放在个人我的微信公众号 《Go编程时光》,对于 Go 语言,我也算是个初学者,所以写的东西应该会比较适合刚接触的同窗,若是你也是刚学习 Go 语言,不防关注一下,一块儿学习,一块儿成长。github
个人在线博客: http://golang.iswbm.com
个人 Github:github.com/iswbm/GolangCodingTime
这一段摘自 Go语言中文网
在面向对象的领域里,接口通常这样定义:接口定义一个对象的行为。接口只指定了对象应该作什么,至于如何实现这个行为(即实现细节),则由对象自己去肯定。golang
在 Go 语言中,接口就是方法签名(Method Signature)的集合。当一个类型定义了接口中的全部方法,咱们称它实现了该接口。这与面向对象编程(OOP)的说法很相似。接口指定了一个类型应该具备的方法,并由该类型决定如何实现这些方法。编程
使用 type 关键字来定义接口。数组
以下代码,定义了一个电话接口,接口要求必须实现 call 方法。微信
type Phone interface { call() }
若是有一个类型/结构体,实现了一个接口要求的全部方法,这里 Phone 接口只有 call方法,因此只要实现了 call 方法,咱们就能够称它实现了 Phone 接口。函数
意思是若是有一台机器,能够给别人打电话,那么咱们就能够把它叫作电话。学习
这个接口的实现是隐式的,不像 JAVA 中要用 implements 显示说明。spa
继续上面电话的例子,咱们先定义一个 Nokia 的结构体,而它实现了 call 的方法,因此它也是一台电话。3d
type Nokia struct { name string } // 接收者为 Nokia func (phone Nokia) call() { fmt.Println("我是 Nokia,是一台电话") }
鸭子类型(Duck typing)的定义是,只要你长得像鸭子,叫起来也像鸭子,那我认为你就是一只鸭子。
举个通俗的例子
什么样子的人能够称作老师呢?
不一样的人标准不一,有的人认为必须有必定的学历,有的人认为必需要有老师资格证。
而我认为只要能育人,能给传授给其余人知识的,均可以称之为老师。
而无论你教的什么学科?是体育竞技,仍是教人烹饪。
也无论你怎么教?是在教室里手执教教鞭、拿着粉笔,仍是追求真实,直接实战演练。
统统无论。
这就一个接口(老师)下,在不一样对象(人)上的不一样表现。这就是多态。
在 Go 语言中,是经过接口来实现的多态。
这里以商品接口来写一段代码演示一下。
先定义一个商品(Good)的接口,意思是一个类型或者结构体,只要实现了settleAccount()
和 orderInfo()
两个方法,那这个类型/结构体就是一个商品。
type Good interface { settleAccount() int orderInfo() string }
而后咱们定义两个结构体,分别是手机和赠品。
type Phone struct { name string quantity int price int } type FreeGift struct { name string quantity int price int }
而后分别为他们实现 Good 接口的两个方法
// Phone func (phone Phone) settleAccount() int { return phone.quantity * phone.price } func (phone Phone) orderInfo() string{ return "您要购买" + strconv.Itoa(phone.quantity)+ "个" + phone.name + "计:" + strconv.Itoa(phone.settleAccount()) + "元" } // FreeGift func (gift FreeGift) settleAccount() int { return 0 } func (gift FreeGift) orderInfo() string{ return "您要购买" + strconv.Itoa(gift.quantity)+ "个" + gift.name + "计:" + strconv.Itoa(gift.settleAccount()) + "元" }
实现了 Good 接口要求的两个方法后,手机和赠品在Go语言看来就都是商品(Good)类型了。
这里候,我挑选了两件商品(实例化),分别是手机和耳机(赠品,不要钱)
iPhone := Phone{ name: "iPhone", quantity: 1, price: 8000, } earphones := FreeGift{ name: "耳机", quantity: 1, price: 200, }
而后建立一个购物车(也就是类型为 Good的切片),来存放这些商品。
goods := []Good{iPhone, earphones}
最后,定义一个方法来计算购物车里的订单金额
func calculateAllPrice(goods []Good) int { var allPrice int for _,good := range goods{ fmt.Println(good.orderInfo()) allPrice += good.settleAccount() } return allPrice }
完整代码,我贴在下面,供你参考。
package main import ( "fmt" "strconv" ) // 定义一个接口 type Good interface { settleAccount() int orderInfo() string } type Phone struct { name string quantity int price int } func (phone Phone) settleAccount() int { return phone.quantity * phone.price } func (phone Phone) orderInfo() string{ return "您要购买" + strconv.Itoa(phone.quantity)+ "个" + phone.name + "计:" + strconv.Itoa(phone.settleAccount()) + "元" } type FreeGift struct { name string quantity int price int } func (gift FreeGift) settleAccount() int { return 0 } func (gift FreeGift) orderInfo() string{ return "您要购买" + strconv.Itoa(gift.quantity)+ "个" + gift.name + "计:" + strconv.Itoa(gift.settleAccount()) + "元" } func calculateAllPrice(goods []Good) int { var allPrice int for _,good := range goods{ fmt.Println(good.orderInfo()) allPrice += good.settleAccount() } return allPrice } func main() { iPhone := Phone{ name: "iPhone", quantity: 1, price: 8000, } earphones := FreeGift{ name: "耳机", quantity: 1, price: 200, } goods := []Good{iPhone, earphones} allPrice := calculateAllPrice(goods) fmt.Printf("该订单总共须要支付 %d 元", allPrice) }
运行后,输出以下
您要购买1个iPhone计:8000元 您要购买1个耳机计:0元 该订单总共须要支付 8000 元
系列导读
24. 超详细解读 Go Modules 前世此生及入门使用