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 oldn",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行, 如果将第42行的注释去掉,我们将得到一个编译错误: 程序剩下的部分不言自明。程序的输出如下: 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行分别声明了两个接口 结构体 在第41行我们将 程序的输出为: 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",}
var empOp EmployeeOperations = e
empOp.DisplaySalary()
fmt.Println("nLeaves left =",empOp.CalculateLeavesLeft())
}
在 Playground 中运行 在上面的程序第15行,通过嵌入 任何一个实现了
在第46行, 程序的输出为: 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 %vn",d1,d1)
}
}
在 Playground 中运行 上面的程序中, d1 is nil and has type <nil> value <nil>
如果我们试图在一个 package main
type Describer interface {
Describe()
}
func main() {
var d1 Describer
d1.Describe()
}
在 Playground 中运行 上面的程序中,因为 panic: runtime error: invalid memory address or nil pointer dereference 接口的介绍就到这里。祝你有美好的一天! 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |