golang 里面有用interface定义接口的,而这个接口似乎并不是其它语言所定义的接口比如:Java python 一个个含义,我在网上看了一个示例代码:
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()
}
为什么能用struct完成组合继承,还需要用interface呢?