Golang教程:(十九)接口 - II

原文:https://golangbot.com/interfaces-part-2/

欢迎来到Golang系列教程的第十九篇。这是介绍接口的第二篇。如果你还没有阅读第一篇,可以在这里阅读。

用指针接收者实现接口

接口 - I 中所有例子都是以值为接收者。也可以使用指针接收者来实现接口。让我们通过一个程序看看这是如何做到的。

package main

import "fmt"

type Describer interface {  
    Describe()
}
type Person struct {  
    name string
    age  int
}

func (p Person) Describe() { //implemented using value receiver  
    fmt.Printf("%s is %d years old\n", p.name, p.age)
}

type Address struct {  
    state   string
    country string
}

func (a *Address) Describe() { //implemented using pointer receiver  
    fmt.Printf("State %s Country %s", a.state, a.country)
}

func main() {  
    var d1 Describer
    p := Person{"Sam", 25}
    d1 = p
    d1.Describe()

    var d2 Describer
    a := Address{"Washington", "USA"}

    /* compilation error if the following line is 
       uncommented
       cannot use a (type Address) as type Describer 
       in assignment: Address does not implement 
       Describer (Describe method has pointer 
       receiver)
    */
    //d2 = a 

    ap := &a
    d2 = ap //This works since Describer interface 
            //is implemented by Address pointer in line 22
    d2.Describe()

}

在 Playground 中运行

在上面的程序中,第13行,Person 结构体以值作为接收者实现了 Describer 接口,而在第22行,Address 结构体以指针作为接收者实现了 Describer 接口。

如果将第42行的注释去掉,我们将得到一个编译错误:main.go:42: cannot use a (type Address) as type Describer in assignment: Address does not implement Describer (Describe method has pointer receiver)。这是因为,在第22行我们使用的是 Address 指针作为接收者来实现 Describer 接口,但是我们试图将一个没有实现 Describer 的接口的值类型的变量 a 赋值给接口变量 d2。第 45 行是合法的,因为我们将指向 a 的指针 ap 赋值给 d2

程序剩下的部分不言自明。程序的输出如下:

Sam is 25 years old  
State Washington Country USA  

实现多个接口

一个类型可以实现多个接口。让我们通过下面的程序看看这是如何做到的。

package main

import (  
    "fmt"
)

type SalaryCalculator interface {  
    DisplaySalary()
}

type LeaveCalculator interface {  
    CalculateLeavesLeft() int
}

type Employee struct {  
    firstName string
    lastName string
    basicPay int
    pf int
    totalLeaves int
    leavesTaken int
}

func (e Employee) DisplaySalary() {  
    fmt.Printf("%s %s has salary $%d", e.firstName, e.lastName, (e.basicPay + e.pf))
}

func (e Employee) CalculateLeavesLeft() int {  
    return e.totalLeaves - e.leavesTaken
}

func main() {  
    e := Employee {
        firstName: "Naveen",
        lastName: "Ramanathan",
        basicPay: 5000,
        pf: 200,
        totalLeaves: 30,
        leavesTaken: 5,
    }
    var s SalaryCalculator = e
    s.DisplaySalary()
    var l LeaveCalculator = e
    fmt.Println("\nLeaves left =", l.CalculateLeavesLeft())
}

在 Playground 中运行

上面的程序在第7行和第11行分别声明了两个接口 SalaryCalculatorLeaveCalculator

结构体 Employee (定义在第15行)在第24行实现了 SalaryCalculator 接口的 DisplaySalary 方法,而在第28行实现了 LeaveCalculator 接口的 CalculateLeavesLeft 方法。现在 Employee 同时实现了 SalaryCalculatorLeaveCalculator 两个接口。

在第41行我们将 e 赋值给 SalaryCalculator 类型的变量,在第43行我们将同样的变量 e 赋值给了 LeaveCalculator 类型的变量。这是合法的,因为 e 的类型是 Employee ,而 Employee 实现了 SalaryCalculatorLeaveCalculator 两个接口。

程序的输出为:

Naveen Ramanathan has salary $5200  
Leaves left = 25 

接口的嵌套

虽然Go没有提供继承机制,但是仍然可以通过嵌入其他接口的方式创建一个新的接口。

下面的程序说明了这一点。

package main

import (  
    "fmt"
)

type SalaryCalculator interface {  
    DisplaySalary()
}

type LeaveCalculator interface {  
    CalculateLeavesLeft() int
}

type EmployeeOperations interface {  
    SalaryCalculator
    LeaveCalculator
}

type Employee struct {  
    firstName string
    lastName string
    basicPay int
    pf int
    totalLeaves int
    leavesTaken int
}

func (e Employee) DisplaySalary() {  
    fmt.Printf("%s %s has salary $%d", e.firstName, e.lastName, (e.basicPay + e.pf))
}

func (e Employee) CalculateLeavesLeft() int {  
    return e.totalLeaves - e.leavesTaken
}

func main() {  
    e := Employee {
        firstName: "Naveen",
        lastName: "Ramanathan",
        basicPay: 5000,
        pf: 200,
        totalLeaves: 30,
        leavesTaken: 5,
    }
    var empOp EmployeeOperations = e
    empOp.DisplaySalary()
    fmt.Println("\nLeaves left =", empOp.CalculateLeavesLeft())
}

在 Playground 中运行

在上面的程序第15行,通过嵌入 SalaryCalculatorLeaveCalculator 两个接口的方式创建了新的接口 EmployeeOperations

任何一个实现了 SalaryCalculatorLeaveCalculator 两个接口的方法的类型,也实现了 EmployeeOperations 接口。

Employee 结构体实现了 EmployeeOperations 接口,因为它在第29行和第33行分别提供了 DisplaySalaryCalculateLeavesLeft 的方法。

在第46行, Employee 类型的 e 被赋值给 EmployeeOperations 类型的 empOp。在下面两行,以 empOp 作为参数调用 DisplaySalary()CalculateLeavesLeft() 方法。

程序的输出为:

Naveen Ramanathan has salary $5200  
Leaves left = 25 

接口的0值

接口的 0 值是 nil。一个 nil 接口的底层类型和值都是 nil。

package main

import "fmt"

type Describer interface {  
    Describe()
}

func main() {  
    var d1 Describer
    if d1 == nil {
        fmt.Printf("d1 is nil and has type %T value %v\n", d1, d1)
    }
}

在 Playground 中运行

上面的程序中,d1 是 nil,程序的输出为:

d1 is nil and has type <nil> value <nil>  

如果我们试图在一个 nil 接口上调用方法,程序将会触发 panic,因为 nil 接口既没底层的值,也没有具体的类型。

package main

type Describer interface {  
    Describe()
}

func main() {  
    var d1 Describer
    d1.Describe()
}

在 Playground 中运行

上面的程序中,因为 d1 是 nil。程序将在运行时触发 panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xc8527]


接口的介绍就到这里。祝你有美好的一天!

目录
上一篇:Golang教程:(十八)接口 - I
下一篇:Golang教程:(二十)并发介绍

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值