go - 接口

go 接口

有点面向对象的意思,只不过所谓的"类"是结构体;
对于一个接口,实例化一个结构体使用new(Struct_type)

实例化一个结构体

phone = new(App)

示例

package main

import (
    "fmt"
)

type Phone interface {
    
    
    call()
}

type NokiaPhone struct {
    
    
}

func (nokiaPhone NokiaPhone) call() {
    
    
    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 main() {
    
    
    var phone Phone

    phone = new(NokiaPhone)
    phone.call()

    phone = new(IPhone)
    phone.call()

}

猜你喜欢

转载自blog.csdn.net/qq_39378657/article/details/112672013