package main

import "fmt"

type Shaper interface {
Area() float32
} type Square struct {
side float32
} func (sq *Square) Area() float32 {
return sq.side * sq.side
} func main() {
sq1 := new(Square)
sq1.side = 5 var areaIntf Shaper
areaIntf = sq1
// shorter,without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
// areaIntf := sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
}

  

上面的程序定义了一个结构体 Square 和一个接口 Shaper,接口有一个方法 Area()

在 main() 方法中创建了一个 Square 的实例。在主程序外边定义了一个接收者类型是 Square 方法的 Area(),用来计算正方形的面积:结构体 Square 实现了接口 Shaper 。

所以可以将一个 Square 类型的变量赋值给一个接口类型的变量:areaIntf = sq1 。

现在接口变量包含一个指向 Square 变量的引用,通过它可以调用 Square 上的方法 Area()。当然也可以直接在 Square 的实例上调用此方法,但是在接口实例上调用此方法更令人兴奋,它使此方法更具有一般性。接口变量里包含了接收者实例的值和指向对应方法表的指针。

这是 多态 的 Go 版本,多态是面向对象编程中一个广为人知的概念:根据当前的类型选择正确的方法,或者说:同一种类型在不同的实例上似乎表现出不同的行为。

如果 Square 没有实现 Area() 方法,编译器将会给出清晰的错误信息:

cannot use sq1 (type *Square) as type Shaper in assignment:
*Square does not implement Shaper (missing Area method)

  如果 Shaper 有另外一个方法 Perimeter(),但是Square 没有实现它,即使没有人在 Square 实例上调用这个方法,编译器也会给出上面同样的错误。

扩展一下上面的例子,类型 Rectangle 也实现了 Shaper 接口。接着创建一个 Shaper 类型的数组,迭代它的每一个元素并在上面调用 Area() 方法,以此来展示多态行为:

package main

import "fmt"

type Shaper interface {
Area() float32
} type Square struct {
side float32
} func (sq *Square) Area() float32 {
return sq.side * sq.side
} type Rectangle struct {
length, width float32
} func (r Rectangle) Area() float32 {
return r.length * r.width
} func main() { r := Rectangle{5, 3} // Area() of Rectangle needs a value
q := &Square{5} // Area() of Square needs a pointer
// shapes := []Shaper{Shaper(r), Shaper(q)}
// or shorter
shapes := []Shaper{r, q}
fmt.Println("Looping through shapes for area ...")
for n, _ := range shapes {
fmt.Println("Shape details: ", shapes[n])
fmt.Println("Area of this shape is: ", shapes[n].Area())
}
}

  在调用 shapes[n].Area() 这个时,只知道 shapes[n] 是一个 Shaper 对象,最后它摇身一变成为了一个 Square 或 Rectangle 对象,并且表现出了相对应的行为。

在调用 shapes[n].Area() 这个时,只知道 shapes[n] 是一个 Shaper 对象,最后它摇身一变成为了一个 Square 或 Rectangle 对象,并且表现出了相对应的行为。

package main

import "fmt"

type stockPosition struct {
ticker string
sharePrice float32
count float32
} /* method to determine the value of a stock position */
func (s stockPosition) getValue() float32 {
return s.sharePrice * s.count
} type car struct {
make string
model string
price float32
} /* method to determine the value of a car */
func (c car) getValue() float32 {
return c.price
} /* contract that defines different things that have value */
type valuable interface {
getValue() float32
} func showValue(asset valuable) {
fmt.Printf("Value of the asset is %f\n", asset.getValue())
} func main() {
var o valuable = stockPosition{"GOOG", 577.20, 4}
showValue(o)
o = car{"BMW", "M3", 66500}
showValue(o)
}

  数据类型实现了接口,就能使用以接口变量为参数的方法

备注:

有的时候,也会以一种稍微不同的方式来使用接口这个词:从某个类型的角度来看,它的接口指的是:它的所有导出方法,只不过没有显式地为这些导出方法额外定一个接口而已。

golang interface接口的更多相关文章

  1. golang面向对象和interface接口

    一. golang面向对象介绍 1.golang也支持面向对象编程,但是和传统的面向对象编程有区别,并不是纯粹的面向对象语言.2.golang没有类(class),golang语言的结合体(struc ...

  2. Golang 入门系列(四)如何理解interface接口

    前面讲了很多Go 语言的基础知识,包括go环境的安装,go语言的语法等,感兴趣的朋友,可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category ...

  3. Golang之接口(interface)

    Golang最重要的接口,,,, package main import ( "fmt" ) //interface类型默认是指针 /* 接口的实现 Golang中的接口,不需要显 ...

  4. Golang基础(8):go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  5. go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  6. [golang note] 接口使用

    侵入式接口 √ 在其他一些编程语言中,接口主要是作为不同组件之间的契约存在,即规定双方交互的规约. √ 对契约的实现是强制的,即必须确保用户的确实现了该接口,而实现一个接口,需要从该接口继承. √ 如 ...

  7. Golang的接口

    当一只鸟走路像鸭子,游泳像鸭子,叫起来也像鸭子,那么我们就认为它就是鸭子. Duck typing 的理念因此比喻得名. Golang 通过 interface 实现 duck typing. Eff ...

  8. golang(08)接口介绍

    原文链接 http://www.limerence2017.com/2019/09/12/golang13/#more 接口简介 golang 中接口是常用的数据结构,接口可以实现like的功能.什么 ...

  9. as3.0 interface接口使用方法

    [转]as3.0 interface接口使用方法 AS在2.0的时候就支持接口了 接口能够让你的程序更具扩展性和灵活性,打个例如 比方你定义了一个方法 代码: public function aMet ...

随机推荐

  1. 解决Qt Creator下 undefined reference to 'qmain(int,char**)'的问题

    Qt Creator运行以下程序: #include <QTextStream> #include <QList> int main(void) { QTextStream o ...

  2. ELK高可用搭建---Elasticsearch配置(1)

    ########################ElasticSearch#######################环境:192.168.125.200   elasticsearch+logst ...

  3. python学习笔记--pycurl模块安装遇到的问题。

    1.用easy_install安装的时候 [root@idayuan ~]# easy_install pycurl Searching for pycurl Best match: pycurl A ...

  4. API网关Kong系列(三)添加服务

    进入之前部署好的kong-ui,默认第一次登陆需要配置kong服务的地址 进入API菜单,点击+号 按照要求填入相关信息 至此完成,可以使用诸如 https://your.domain.com:208 ...

  5. jQuery的get()用法

    这个方法主要是将jQuery对象或者jQuery对象集合转换成DOM对象或dom对象集合. get()方法中如果传递参数,表示将具体位置的jQuery对象转换成dom对象.如果没有参数,则表示返回所有 ...

  6. TensorFlow系列专题(一):机器学习基础

  7. Github入门 - Github基本使用及Github桌面版使用

    知识内容: 1.版本控制 2.Git介绍 3.Github介绍及基本使用 4.Github桌面版介绍及安装 5.Github桌面版基础使用 6.Github桌面版进阶使用 参考: http://www ...

  8. python拓展4 数据结构

    内容: 1.数组 2.链表 3.字典 4.二叉树(搜索树) 5.set集合实现 1.数组 数组在python中是以列表的形式存在,基本上每一个语言中都有数组形式的数据结构存在 数组一般存储在连续的一块 ...

  9. python入门-IF语句

    1 格式 cars = ['audi','bmw','subaru','toyata'] for car in cars: if car =='bmw': print(car.upper()) els ...

  10. 网络性能测试工具iperf

    参考网站:https://www.cnblogs.com/yingsong/p/5682080.html https://docs.azure.cn/zh-cn/articles/azure-oper ...