Go 语言接口
Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。
接口可以让我们将不同的类型绑定到一组公共的方法上,从而实现多态和灵活的设计。
Go 语言中的接口是隐式实现的,也就是说,如果一个类型实现了一个接口定义的所有方法,那么它就自动地实现了该接口。因此,我们可以通过将接口作为参数来实现对不同类型的调用,从而实现多态。
实例
实例
/* 定义接口 */
type interface_name interface {
method_name1 [return_type]
method_name2 [return_type]
method_name3 [return_type]
...
method_namen [return_type]
}
/* 定义结构体 */
type struct_name struct {
/* variables */
}
/* 实现接口方法 */
func (struct_name_variable struct_name) method_name1() [return_type] {
/* 方法实现 */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
/* 方法实现*/
}
type interface_name interface {
method_name1 [return_type]
method_name2 [return_type]
method_name3 [return_type]
...
method_namen [return_type]
}
/* 定义结构体 */
type struct_name struct {
/* variables */
}
/* 实现接口方法 */
func (struct_name_variable struct_name) method_name1() [return_type] {
/* 方法实现 */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
/* 方法实现*/
}
实例
以下两个实例演示了接口的使用:
实例 1
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 iPhone, I can call you!")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
}
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 iPhone, I can call you!")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
}
在上面的例子中,我们定义了一个接口 Phone,接口里面有一个方法 call()。然后我们在 main 函数里面定义了一个 Phone 类型变量,并分别为之赋值为 NokiaPhone 和 IPhone。然后调用 call() 方法,输出结果如下:
I am Nokia, I can call you! I am iPhone, I can call you!
第二个接口实例:
实例
package main
import "fmt"
type Shape interface {
area() float64
}
type Rectangle struct {
width float64
height float64
}
func (r Rectangle) area() float64 {
return r.width * r.height
}
type Circle struct {
radius float64
}
func (c Circle) area() float64 {
return 3.14 * c.radius * c.radius
}
func main() {
var s Shape
s = Rectangle{width: 10, height: 5}
fmt.Printf("矩形面积: %f\n", s.area())
s = Circle{radius: 3}
fmt.Printf("圆形面积: %f\n", s.area())
}
import "fmt"
type Shape interface {
area() float64
}
type Rectangle struct {
width float64
height float64
}
func (r Rectangle) area() float64 {
return r.width * r.height
}
type Circle struct {
radius float64
}
func (c Circle) area() float64 {
return 3.14 * c.radius * c.radius
}
func main() {
var s Shape
s = Rectangle{width: 10, height: 5}
fmt.Printf("矩形面积: %f\n", s.area())
s = Circle{radius: 3}
fmt.Printf("圆形面积: %f\n", s.area())
}
以上实例中,我们定义了一个 Shape 接口,它定义了一个方法 area(),该方法返回一个 float64 类型的面积值。然后,我们定义了两个结构体 Rectangle 和 Circle,它们分别实现了 Shape 接口的 area() 方法。在 main() 函数中,我们首先定义了一个 Shape 类型的变量 s,然后分别将 Rectangle 和 Circle 类型的实例赋值给它,并通过 area() 方法计算它们的面积并打印出来,输出结果如下:
矩形面积: 50.000000 圆形面积: 28.260000
需要注意的是,接口类型变量可以存储任何实现了该接口的类型的值。在示例中,我们将 Rectangle 和 Circle 类型的实例都赋值给了 Shape 类型的变量 s,并通过 area() 方法调用它们的面积计算方法。
Webben
wei***_li@126.com
给接口增加参数:
package main import ( "fmt" ) type Man interface { name() string; age() int; } type Woman struct { } func (woman Woman) name() string { return "Jin Yawei" } func (woman Woman) age() int { return 23; } type Men struct { } func ( men Men) name() string { return "liweibin"; } func ( men Men) age() int { return 27; } func main(){ var man Man; man = new(Woman); fmt.Println( man.name()); fmt.Println( man.age()); man = new(Men); fmt.Println( man.name()); fmt.Println( man.age()); }Webben
wei***_li@126.com
杀手
zho***ng920@foxmail.com
func (name string) imp() string{ print("这是实现方法的写法") } func sum(x int,y int) int{ print("这是正常写法") }杀手
zho***ng920@foxmail.com
mrstone
dam***tone@163.com
接口方法传参,以及返回结果:
package main import "fmt" type Phone interface { call(param int) string takephoto() } type Huawei struct { } func (huawei Huawei) call(param int) string{ fmt.Println("i am Huawei, i can call you!", param) return "damon" } func (huawei Huawei) takephoto() { fmt.Println("i can take a photo for you") } func main(){ var phone Phone phone = new(Huawei) phone.takephoto() r := phone.call(50) fmt.Println(r) }mrstone
dam***tone@163.com
啊啊啊
644***5693@qq.com
接口案例:
package main import ( "fmt" ) //定义接口 type Phone interface { call() call2() } //一直都搞不懂这是干啥的 //原来是用来定义结构体内的数据类型的 type Phone1 struct { id int name string category_id int category_name string } //第一个类的第一个回调函数 func (test Phone1) call() { fmt.Println("这是第一个类的第一个接口回调函数 结构体数据:", Phone1{id: 1, name: "浅笑"}) } //第一个类的第二个回调函数 func (test Phone1) call2() { fmt.Println("这是一个类的第二个接口回调函数call2", Phone1{id: 1, name: "浅笑", category_id: 4, category_name: "分类名称"}) } //第二个结构体的数据类型 type Phone2 struct { member_id int member_balance float32 member_sex bool member_nickname string } //第二个类的第一个回调函数 func (test2 Phone2) call() { fmt.Println("这是第二个类的第一个接口回调函数call", Phone2{member_id: 22, member_balance: 15.23, member_sex: false, member_nickname: "浅笑18"}) } //第二个类的第二个回调函数 func (test2 Phone2) call2() { fmt.Println("这是第二个类的第二个接口回调函数call2", Phone2{member_id: 44, member_balance: 100, member_sex: true, member_nickname: "陈超"}) } //开始运行 func main() { var phone Phone //先实例化第一个接口 phone = new(Phone1) phone.call() phone.call2() //实例化第二个接口 phone = new(Phone2) phone.call() phone.call2() }啊啊啊
644***5693@qq.com
mu_wangyue
zyj***163.com
将接口做为参数
package main import ( "fmt" ) type Phone interface { call() string } type Android struct { brand string } type IPhone struct { version string } func (android Android) call() string { return "I am Android " + android.brand } func (iPhone IPhone) call() string { return "I am iPhone " + iPhone.version } func printCall(p Phone) { fmt.Println(p.call() + ", I can call you!") } func main() { var vivo = Android{brand:"Vivo"} var hw = Android{"HuaWei"} i7 := IPhone{"7 Plus"} ix := IPhone{"X"} printCall(vivo) printCall(hw) printCall(i7) printCall(ix) }输出结果:
mu_wangyue
zyj***163.com
cdy1996
138***68445@163.com
如果想要通过接口方法修改属性,需要在传入指针的结构体才行,具体代码入下的 [1][2] 处:
type fruit interface{ getName() string setName(name string) } type apple struct{ name string } //[1] func (a *apple) getName() string{ return a.name } //[2] func (a *apple) setName(name string) { a.name = name } func testInterface(){ a:=apple{"红富士"} fmt.Print(a.getName()) a.setName("树顶红") fmt.Print(a.getName()) }cdy1996
138***68445@163.com
莫莫
923***919@qq.com
带参数的实现:
package main import "fmt" type Animal interface { eat() } type Cat struct { name string } func (cat Cat) eat(){ fmt.Println(cat.name +"猫吃东西" ) } type Dog struct{} func (dog Dog) eat(){ fmt.Println("狗吃东西") } func main() { var animal1 Animal=Cat{"maomao"} var animal2 Animal=Dog{} animal1.eat() animal2.eat() }莫莫
923***919@qq.com
sharkshark
123***st.com
组合接口:
package main import "fmt" type reader interface { read() string } type writer interface { write() string } type rw interface { reader writer } type mouse struct{} func (m mouse) read() string { return "mouse reading..." } func (m *mouse) write() string { return "mouse writing..." } func main() { var rw1 rw // 只要有一个指针实现,则此处必须是指针 rw1 = &mouse{} // rw1 = new(mouse) fmt.Println(rw1.read()) fmt.Println(rw1.write()) }sharkshark
123***st.com
haolog
zha***ao6991@163.com
interface 本质是一个指针:
package main import "fmt" type Reader interface { // interface本质就是一个指针 ReadBook() } type Writer interface { WriteBook() } type Book struct{ } func (t *Book) ReadBook() { fmt.Println("read a book") } func (t *Book) WriteBook() { fmt.Println("write a book") } func main() { // b : pair<type:Book, value:book{}地址> b := &Book{} // r: pair<type, value> var r Reader // r : pair<type:Book, value:book{}地址> pair是不变的 r = b // interface r 本质就是一个指针,所以b也要是指针类型 r.ReadBook() var w Writer // w : pair<type:Book, value:book{}地址> pair是不变的 w = r.(Writer) // w和r的type一致,所以断言成功(断言就是强转?) w.WriteBook() }haolog
zha***ao6991@163.com