Interface - 初探 Golang 中的接口(一)

黑痕 · · 853 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

接口(interface ),是对其他类型行为的概括和抽象。

1. 类型行为的概括和抽象

假设对长方形、圆或者三角形等形状进行行为的概括和抽象,不难想到它们有一个共同的行为:在二维面求面积!

不考虑它们是什么类型,我们在包shape.go中定义一个接口,并用该接口AreaTeller作为参数实现了TellArea方法:

package shape

import "fmt"

type AreaTeller interface {
    Area() float64
}

func TellArea(at AreaTeller) {
    fmt.Printf("Hello, my area is %.2f\n", at.Area())
}


2. 接口的调用和实现

假设现在有一个长方形 R 和圆 C,让 shape 中的函数 TellArea 都可以把他们作为参数进行输出,即:shape.TellArea(R)shape.TellArea(C) 并预期得到我们想要的结果,该如何实现呢?

首先在 main.go 文件中,定义长方形和圆的结构描述:

type Rectangle struct {
    X, Y float64
}

type Circle struct {
    R float64
}

此时我们直接调用,直接报错:

package main

import (
    "github.com/alienrick/00_test/area"
)

type Rectangle struct {
    X, Y float64
}

type Circle struct {
    R float64
}

func main() {
    r := Rectangle{X: 3, Y: 4}
    shape.TellArea(r) // error

    c := Circle{R: 3}
    shape.TellArea(c) // error
}

因为 shape包中的 AreaTeller接口 Area() float 对所有形状的求面积行为进行抽象,假使让AreaTeller方法可以把长方形和圆作为参数传入,就必须实现Area() float64方法,如下:

func (r Rectangle) Area() float64 {
    return r.X * r.Y
}

func (c Circle) Area() float64 {
    return math.Pi * c.R * c.R
}

在 golang 的术语中,这就是接口的隐式实现

package main

import (
    "math"

    "github.com/alienrick/00_test/shape"
)

type Rectangle struct {
    X, Y float64
}

func (r Rectangle) Area() float64 {
    return r.X * r.Y
}

func (c Circle) Area() float64 {
    return math.Pi * c.R * c.R
}

type Circle struct {
    R float64
}

func main() {
    r := Rectangle{X: 3, Y: 4}
    shape.TellArea(r)

    c := Circle{R: 3}
    shape.TellArea(c)
}

运行如上代码,得出结果:

Hello, my area is 12.00
Hello, my area is 28.27

3.扩展

假使现在来了一个三角形T,该怎么扩展呢?

type Square struct {
    X float64
}

func (t Square) Area() float64 {
    return t.X * t.X
} 

func main(){
    s := Square{X: 3}
    shape.TellArea(s) // Hello, my area is 9.00
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:黑痕

查看原文:Interface - 初探 Golang 中的接口(一)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

853 次点击  
加入收藏 微博
上一篇:Go Modules 详解
下一篇:Go web 教程
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传