关于go:Golang-接口与多态

5次阅读

共计 277 个字符,预计需要花费 1 分钟才能阅读完成。

假如我有一个 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()}
正文完
 0