// interface.go
package main
import (
"fmt"
)
type Phone interface {
call()
}
type NokiaPhone struct {
}
type IPhone struct {
}
type Android struct {
}
func (nokia NokiaPhone) call() {
fmt.Println("I am Nokia!")
}
func (ios IPhone) call() {
fmt.Println("I am IPhone!")
}
func (and Android) call() {
fmt.Println("I am Android!")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
phone = new(Android)
phone.call()
}
输出结果:
I am Nokia!
I am IPhone!
I am Android!
有疑问加站长微信联系(非本文作者)