Go 语言接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boyun58/article/details/88874721

o 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。

例如

package main 

import (
	"fmt"
)

type Phone interface {
	call()
}


type NokiaPhone struct {

}


func (nokiaPhone NokiaPhone) call() {
	fmt.Println("I am Nokia, I can call you!")
}


type IPhone struct {

}


func (iPhone IPhone) call() {
	fmt.Println("I am iPone, I can call you!")
}


func main() {
	var phone Phone
	phone = new(NokiaPhone)
	phone.call()

	phone = new(IPhone)
	phone.call()
}
----------------------------------------------------------
I am Nokia, I can call you!
I am iPone, I can call you!

猜你喜欢

转载自blog.csdn.net/boyun58/article/details/88874721