Golang 接口与多态

假设我有一个HUAWEI类,要实现Phone接口,可以这样做:

package main

import "fmt"

//定义一个接口
type Phone interface {
    call()
}

//定义一个类
type HUAWEI struct {
    name string
}

//用上面的HUAWEI类实现上面的Phone接口
func (myPhone HUAWEI) call() {
    fmt.Println("this is the coolest phone all around the world")
}

func main() {
    thisPhone := new(HUAWEI)
    thisPhone.call()
}

你可能感兴趣的:(go接口多态)