最近又在用go, 来看个简单的程序:
package main
import (
"fmt"
)
type BaseIntf interface {
f()
}
type D struct {
x int
}
func (this *D)f() {
this.x = 200
}
func main() {
var d D
d.x = 100
var b BaseIntf = &d
fmt.Printf("%T, %v\n", b, d)
b.f()
fmt.Printf("%T, %v\n", b, d)
// b.x = 1 // b.x undefined (type BaseIntf has no field or method x)
}
这就体现了go的多态,有兴趣的朋友可以运行一下。