go - 如何调用实现接口(interface)的结构的特定方法

标签 go interface

我有以下接口(interface)和一些实现它的结构:

package main

import "fmt"

type vehicle interface {
    vehicleType() string
    numberOfWheels() int
    EngineType() string
}

// -------------------------------------------

type truck struct {
    loadCapacity int
}

func (t truck) vehicleType() string {
    return "Truck"
}

func (t truck) numberOfWheels() int {
    return 6
}

func (t truck) EngineType() string {
    return "Gasoline"
}

// -------------------------------------------
type ev struct {
    capacityInKWh int
}

func (e ev) vehicleType() string {
    return "Electric Vehicle"
}

func (e ev) numberOfWheels() int {
    return 4
}

func (e ev) EngineType() string {
    return "Electric"
}

func (e ev) Capacity() int {
    return e.capacityInKWh
}

// -------------------------------------------

type dealer struct{}

func (d dealer) sell(automobile vehicle) {
    fmt.Println("Selling a vehicle with the following properties")
    fmt.Printf("Vehicle Type: %s \n", automobile.vehicleType())
    fmt.Printf("Vehicle Number of wheels: %d \n", automobile.numberOfWheels())
    fmt.Printf("Vehicle Engine Type: %s \n", automobile.EngineType())

    if automobile.EngineType() == "Electric" {
        fmt.Printf("The battery capacity of the vehicle is %d KWh", automobile.Capacity())
        //fmt.Printf("Here")
    }
}

func main() {

    volvoTruck := truck{
        loadCapacity: 10,
    }

    tesla := ev{
        capacityInKWh: 100,
    }

    myDealer := dealer{}
    myDealer.sell(volvoTruck)
    fmt.Println("---------------------------")
    myDealer.sell(tesla)

}
Sell我的 dealer{} 中的方法struct 接收一个接口(interface)。在这个方法中,我想调用一个只存在于实现接口(interface)的结构之一上而不存在于其他结构上的方法:
if automobile.EngineType() == "Electric" {
            fmt.Printf("The battery capacity of the vehicle is %d KWh", automobile.Capacity())
        }
请注意 Capacity()仅存在于 ev{}但不在 truck{} 中.有没有办法做到这一点,而不必将此方法添加到强制所有实现使用它的接口(interface)?

最佳答案

您可以使用 type assertion 检查方法是否存在.检查该值(或更具体地说,它的类型)是否具有您要查找的方法,如果有,您可以调用它。
可以通过检查值是否使用该单一方法实现接口(interface)来实现检查方法:

if hc, ok := automobile.(interface {
    Capacity() int
}); ok {
    fmt.Printf("The battery capacity of the vehicle is %d KWh", hc.Capacity())
}
然后输出将是(在 Go Playground 上尝试):
Selling a vehicle with the following properties
Vehicle Type: Truck 
Vehicle Number of wheels: 6 
Vehicle Engine Type: Gasoline 
---------------------------
Selling a vehicle with the following properties
Vehicle Type: Electric Vehicle 
Vehicle Number of wheels: 4 
Vehicle Engine Type: Electric 
The battery capacity of the vehicle is 100 KWh
如果为它创建一个命名的接口(interface)类型会更好:
type HasCapacity interface {
    Capacity() int
}
进而:
if hc, ok := automobile.(HasCapacity); ok {
    fmt.Printf("The battery capacity of the vehicle is %d KWh", hc.Capacity())
}
输出将是相同的,在 Go Playground 上试试这个.

关于go - 如何调用实现接口(interface)的结构的特定方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363073/

相关文章:

html - 如何使用 golang 运行带有 css 的 html

c# - 接口(interface)的使用,实际和现实世界的例子

go - 了解字符串转义序列

戈朗 : Error while make test: signal: killed

java - 如何保证实现接口(interface)的类也扩展了类?

php - ArrayAccess 多维(非)集?

c# - 同名接口(interface)中的属性C#

C#:在接口(interface)中实现抽象属性以实现子级

go - 如何在HTTP请求后使用pprof查看Web服务器的内存使用情况

json - 在 struct 标签中使用变量