go 接口
go interface
Go 语言不是一种 “传统” 的面向对象编程语言:它里面没有 但是 Go 语言里有非常灵活的
定义格式定义接口的一般格式: type Namer interface {
Method1(param_list) return_type
Method2(param_list) return_type
...
}
上面的 在 Go 语言中
package main
import "fmt"
type Shaper interface {
Area() float64
// Perimeter() float64
}
type Rectangle struct {
length float64
width float64
}
// 实现 Shaper 接口中的方法
func (r *Rectangle) Area() float64 {
return r.length * r.width
}
// Set 是属于 Rectangle 自己的方法
func (r *Rectangle) Set(l float64,w float64) {
r.length = l
r.width = w
}
func main() {
rect := new(Rectangle)
rect.Set(2, 3)
areaIntf := Shaper(rect)
fmt.Printf("The rect has area: %fn",areaIntf.Area())
}
如果去掉 cannot convert rect (type *Rectangle) to type Shaper: *Rectangle does not implement Shaper (missing Perimeter method)
多态
下面我们增加一个类型 package main
import "fmt"
type Shaper interface {
Area() float64
// Perimeter() float64
}
// ==== Rectangle ====
type Rectangle struct {
length float64
width float64
}
// 实现 Shaper 接口中的方法
func (r *Rectangle) Area() float64 {
return r.length * r.width
}
// Set 是属于 Rectangle 自己的方法
func (r *Rectangle) Set(l float64,w float64) {
r.length = l
r.width = w
}
// ==== Rectangle End ====
// ==== Triangle ====
type Triangle struct {
bottom float64
hight float64
}
func (t *Triangle) Area() float64 {
return t.bottom * t.hight / 2
}
func (t *Triangle) Set(b float64,h float64) {
t.bottom = b
t.hight = h
}
// ==== Triangle End ====
func main() {
rect := new(Rectangle)
rect.Set(2,areaIntf.Area())
triangle := new(Triangle)
triangle.Set(2, 3)
areaIntf = Shaper(triangle)
fmt.Printf("The triangle has area: %fn",areaIntf.Area())
}
灵活性接口的定义是比较灵活的。
现在我们将 [root@ src]# tree
├── test
│ └── test.go
├── main.go
└── shaper
└── shaper.go
// shaper.go
package shaper
type Shaper interface {
Area() float64
}
// test.go
package test
// ==== Rectangle ====
type Rectangle struct {
length float64
width float64
}
// 实现 Shaper 接口的方法
func (r *Rectangle) Area() float64 {
return r.length * r.width
}
// Set 是属于 Rectangle 自己的方法
func (r *Rectangle) Set(l float64,h float64) {
t.bottom = b
t.hight = h
}
// ==== Triangle End ====
// main.go
package main
import (
"fmt"
"shaper"
"test"
)
func main() {
rect := new(test.Rectangle)
rect.Set(2, 3)
areaIntf := shaper.Shaper(rect)
fmt.Printf("The rect has area: %fn",areaIntf.Area())
triangle := new(test.Triangle)
triangle.Set(2, 3)
areaIntf = shaper.Shaper(triangle)
fmt.Printf("The triangle has area: %fn",areaIntf.Area())
}
现在运行 The rect has area: 6.000000
The triangle has area: 3.000000
接口嵌套一个接口可以包含一个或多个其他的接口,这相当于直接将这些内嵌接口的方法列举在外层接口中一样。 比如接口 type ReadWrite interface {
Read(b Buffer) bool
Write(b Buffer) bool
}
type Lock interface {
Lock()
Unlock()
}
type File interface {
ReadWrite
Lock
Close()
}
类型断言假如我现在写了一个结构体类型 通常我们使用 if v,ok : = varI.(T) ; ok { // checked type assertion
Process(v)
return
}
// varI is not of type T
如果 // main.go
package main
import "fmt"
type MyFile struct{}
func (m *MyFile) Read() bool {
fmt.Printf("Read()n")
return true
}
// ...
// 假设我这里相继实现了 Write(),Lock(),Unlock() 和 Close() 方法
func main() {
my := new(MyFile)
fIntf := File(my)
// 看这里,看这里
if v,ok := fIntf.(*MyFile); ok {
v.Read()
}
}
输出结果是:Read() 要是多个类型实现了同一个接口,比如前面的 type-switch 类型判断switch t := areaIntf.(type) {
case *Rectangle:
// do something
case *Triangle:
// do something
default:
// do something
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |