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面试宝典(转)
Java知识点包括:接口与抽象的使用及区别,多线程,socket基础,集合类,也有个别公司考察定义,很无语. C/C++知识点包括:指针的移动,排序算法,链表,有时还会有二叉树的遍历或图的遍历. 1. ...
- red5研究(一):下载,工程建立、oflaDemo安装、demo测试
一.red5下载.添加工程到myeclipse 1,从官网上下载red51.01版本(我下载的是red51.0的版本),下载链接http://www.red5.org/downloads/red5/1 ...
- atomikos的Jta配置
配置说明见: http://www.atomikos.com/Documentation/JtaProperties atomikos的一些配置,文档中说明的比较清楚,有两个属性配置不太明确:com. ...
- 最受欢迎的5个Android ORM框架
在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...
- hdr_beg(host) hdr_reg(host) hdr_dom(host)
case 1 测试hdr_beg(host) 的情况 acl zjtest7_com hdr_beg(host) -i zjtest7.com use_backend zjtest7_com if z ...
- ubuntu装机
备份: .bashrc profile .vimrc exports defults/ 各种workspace中的源码 goagent/ 重转后安装: apt-get install openjdk- ...
- 在Ubuntu 12.04安装和设置SSH服务
1.安装 Ubuntu缺省安装了openssh-client,所以在这里就不安装了,如果你的系统没有安装的话,再用apt-get安装上即可. 安装ssh-server sudo apt-get ins ...
- Grunt 初体验
对于没有接触过类似自动化工具的朋友,对 grunt 也许只是停留在听过阶段,而并没有真正的使用过.今天就从最初级的教程说起.在开始教程之前,需要先确保你已经安装了 node. 下面就开始来讲解 gru ...
- sublime text2卸载和重新安装
很多同学使用 sublime text2 的时候,出现一些奇怪的bug,且重启无法修复. 于是,就会想到卸载 sublime text2 再重新安装. 然而,你会发现,重新安装后,这个bug任然存在, ...
- [原]Unity3D深入浅出 - 物理材质(Physics Materials)
在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...