package main import ( "fmt" ) type BinaryTree interface { Set(value interface{}) getLeft() BinaryTree getRight() BinaryTree Value() interface{} } type Test interface { BinaryTree } type IntBinaryTree struct { value int left Test right Test } func (a *IntBinaryTree) Set(value interface{}) { a.value = value.(int) } func (a *IntBinaryTree) getLeft() BinaryTree { return a.left }
例:func (a *IntBinaryTree) getRight() IntBinaryTree { return a.right } 而不是接口中声明的类型,编译是通不过的 严格的返回接口中的定义的类型, 而不能是实现返回接口的具体类型()
func (a *IntBinaryTree) Value() interface{} { return a.value } func main() { var a BinaryTree = &IntBinaryTree{} var b Test = &IntBinaryTree{} a.Set(19) b = a.(Test) fmt.Println(a.Value()) fmt.Println(b.Value()) }