美文网首页
Interface - 初探 Golang 中的接口(一)

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

作者: 黑痕 | 来源:发表于2019-06-21 01:08 被阅读0次

接口(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 中的接口(一)

      本文链接:https://www.haomeiwen.com/subject/lsxeqctx.html