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. 用过的sql 工具

    sequel pro 港优创新 php myadmin 腾讯

  2. 针对IE6浏览器下,zoom:1的问题

    一.css代码如下: .message .con .word {font-size:14px;color:#333333; border-radius:3px; padding:10px;border ...

  3. ZooKeeper 集群的安装、配置---Dubbo 注册中心

    ZooKeeper 集群的安装.配置.高可用测试 Dubbo 注册中心集群 Zookeeper-3.4.6 Dubbo 建议使用 Zookeeper 作为服务的注册中心. Zookeeper 集群中只 ...

  4. nsenter工具进入docker容器

    对于运行在后台的Docker容器,我们经常需要做的事情是进入到容器中,docker为我们提供了docker exec .docker attach 命令,并且还提供了nsenter工具,外部工具供我们 ...

  5. Dubbo与Zookeeper、Spring整合使用

    Dubbo与Zookeeper.Spring整合使用 Dubbo采用全spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...

  6. 输出tuple和chrono的使用小例子

    // move example #include <iostream> // std::cout #include<tuple> #include<ratio> # ...

  7. IDEA设置syso快捷键输出System.out.println();

    用Eclipse时间长了, 就习惯之前的快捷键! 当然, IDEA不愧是Java开发的”利器”! 写起代码就是一个字 – “爽”! 建议大家可以去尝试一下! 当然, 在IDEA中输出System.ou ...

  8. filter实例

    最近在学习filter,看完理论知识之后,就特地在实际工作中的系统中去验证. SSP系统实例如下: 1.在web.xml中寻找 filter的配置信息 如下: <filter>  < ...

  9. checkbox的美化(转)

    http://www.w3cfuns.com/blog-5422889-5398674.html <!DOCTYPE html> <html> <head> < ...

  10. java 项目中类找不到异常解决办法

    最后点击Apply and Close就可以了