关于go: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()
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理