博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Tour of Go Interfaces
阅读量:6942 次
发布时间:2019-06-27

本文共 987 字,大约阅读时间需要 3 分钟。

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 mainimport (    "fmt"    "math") type Abser interface {     Abs() float64 } func main() {     var a Abser     f := MyFloat(-math.Sqrt2)     v := Vertex{
3, 4} 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 < 0 { 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) }

接口现在的看法越来越是只要这个类 对象 结构或者任何东西它实现了某一些方法组合那么他就实现了这个接口

转载于:https://www.cnblogs.com/ghgyj/p/4057694.html

你可能感兴趣的文章
前后端分离?
查看>>
elasticsearch接口开发(新)
查看>>
Django&Admin站点&调整站点信息
查看>>
POJ2125 Destroying The Graph
查看>>
观察者模式
查看>>
react router browserhistory 关于 Nginx配置
查看>>
Focal Loss 的前向与后向公式推导
查看>>
PostgreSQL远端访问
查看>>
家庭里如何搭建一个互联网可访问的服务器
查看>>
eclipse与SVN 结合(删除SVN中已经上传的问题)
查看>>
深入理解Fsync
查看>>
去掉xcode编译warning:ld: warning: directory not found for option '-L
查看>>
杭电1702--ACboy needs your help again!
查看>>
web前端开发分享-css,js进阶篇
查看>>
安大校赛/异或运算一题。
查看>>
强制回收和IDisposable.Dispose方法
查看>>
mybatis plus条件构造器
查看>>
quick sort(重复数版)
查看>>
乌班图 root权限获取
查看>>
Java内部类
查看>>