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{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)
 }

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

时间: 2024-08-11 01:55:45

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 intent. Implicit interfaces decouple implementation packages from the packages that define the interfaces: neither depends on the other. It also encourage

Go Methods and Interfaces

[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name. 2.You can declare a method on any type that is

18 A GIF decoder: an exercise in Go interfaces

A GIF decoder: an exercise in Go interfaces 25 May 2011 Introduction At the Google I/O conference in San Francisco on May 10, 2011, we announced that the Go language is now available on Google App Engine. Go is the first language to be made available

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

disable NetworkManager and boot on static ip configuration using /etc/network/interfaces

1.stop and disable NetworkManager sudo systemctl stop NetworkManager sudo systemctl disable NetworkManager 2.drop status of NetworkManger sudo mv /var/lib/NetworkManager/NetworkManager.state /var/lib/NetworkManager/NetworkManager.state.bak 3.edit int

Codeforces 490F Treeland Tour(离散化 + 线段树合并)

题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N =