A Tour of Go Interfaces
An interface type is defined by a set of methods.
A value of interface type can hold any value that implements those methods.
Note: The code on the left fails to compile.
Vertex
doesn't satisfy Abser
because the Abs
method is defined only on*Vertex
, not Vertex
.
package main import (
"fmt"
"math"
)
type Abser interface {
Abs() float64
} func main() {
var a Abser
f := MyFloat(-math.Sqrt2)
v := Vertex{, } a = f // a MyFloat implements Abser
a = &v // a *Vertex implements Abser // In the following line, v is a Vertex(not *Vertex)
// and does Not Implement Abser.
//a = v
fmt.Println(a.Abs())
} type MyFloat float64 func (f MyFloat) Abs() float64 {
if f < {
return float64(-f)
}
return float64(f)
} type Vertex struct {
X,Y float64
} func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
接口现在的看法越来越是只要这个类 对象 结构或者任何东西它实现了某一些方法组合那么他就实现了这个接口
A Tour of Go Interfaces的更多相关文章
- A Tour of Go Interfaces are satisfied implicitly
A type implements an interface by implementing the methods. There is no explicit declaration of inte ...
- A Tour of Go Methods and Interfaces
The next group of slides covers methods and interfaces, the constructs that define objects and their ...
- go官网教程A Tour of Go
http://tour.golang.org/#1 中文版:http://go-tour-cn.appsp0t.com/#4 package main import ( "fmt" ...
- 18 A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练
A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练 25 May 2011 Introduction At the Googl ...
- Go Methods and Interfaces
[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...
- 后端程序员之路 53、A Tour of Go-3
#method - Methods - Go does not have classes. However, you can define methods on types. ...
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- 【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
I got the following error message when trying to open a network interface for capture using Wireshar ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
随机推荐
- android 图片画画板
canvas.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- linux压缩文件(夹) zip uzip命令的用法
压缩文件(夹) # 压缩列举的文件,格式如下: zip 压缩包名称 文件1 文件2 文件3 ... # 压缩test.txt, a.out文件,并取名为abc.zip $ zip abc.zip te ...
- live555源码研究(三)------UsageEnvironment类
一.UsageEnvironment类作用 1,不使用的时候回收当前的使用环境. 2,对返回结果消息和错误消息的维护. 二.类UsageEnvironment继承关系图
- Qt之QtSoap(访问WebService)
http://blog.csdn.net/u011012932/article/details/51673800
- SRM 585 DIV1 L2
记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...
- Android 使用split函数进行多个空格分割
在项目中经常会遇到按字符分割字符串的情况,可以使用String对象的split函数进行分割. 先看实际情况: String str = "关键词1 关键词2 关键词3"; Stri ...
- C++内存中的封装、继承、多态(下)
上篇讲述了内存中的封装模型,下篇我们讲述一下继承和多态. 二.继承与多态情况下的内存布局 由于继承下的内存布局以及构造过程很多书籍都讲得比较详细,所以这里不细讲.重点讲多态. 继承有以下这几种情况: ...
- android SharedPreferences apply和commit的区别
1.apply没有返回值而commit返回boolean表明修改是否提交成功2.apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘3.apply ...
- 【转】parallels desktop 11 授权许可文件删除方法
原文网址:http://www.macappstore.net/tips/parallels-desktop-uninstall/ 很多同学在安装parallels desktop 11破解版后显示还 ...
- andori 动画验证必填项
android项目开发过程中,都会碰到必填项的校验,最简单的就是利用Toast对用进行提示,感觉这种提示太不够人性化了,那么今天就来个带动画的,并可以将光标定位到必填项中. andorid动画Anim ...