答案:和java一样,需要全部实现所有接口方法,如果不实现所有接口方法,意味着没有继承这个接口。

package main
 
import (
	"fmt"
)
 
type Phone interface {
	call()
	start()
}
 
type NokiaPhone struct {
}
 
func (nokiaPhone NokiaPhone) call() {
	fmt.Println("I am Nokia, I can call you!")
}
 
func (nokiaPhone NokiaPhone) start() {
	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 (iPhone IPhone) start() {
	fmt.Println("I am iPhone, I can start you!")
}
 
func main() {
	var phone Phone
 
	phone = new(NokiaPhone)
	phone.call()
 
	phone = new(IPhone)
	phone.call()
 
}
Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐