Golang 接口(interface)

原创比较累,希望大家能点点赞,对我的支持。你们的支持,就是我的动力。

1. 接口实现

在 Go 中,接口是一种抽象类型,它定义了一组方法签名,但没有实现。接口用于描述对象应该具有的方法集合,而不是具体的实现方式。

接口的定义使用 `type` 和 `interface` 关键字。例如:


type MyInterface interface {
    Method1() int
    Method2(string) string
}
 

上面的代码定义了一个名为 `MyInterface` 的接口,它包含两个方法 `Method1` 和 `Method2`,一个返回 `int` 类型,另一个返回 `string` 类型。

接口的实现是通过创建实现了接口中所有方法的具体类型来完成的。在 Go 中,一个类型只要实现了接口中定义的所有方法,就被认为是实现该接口的类型。例如:


type MyStruct struct {
    // ...
}

func (s MyStruct) Method1() int {
    // ...
}

func (s MyStruct) Method2(str string) string {
    // ...
}

var myVar MyInterface = MyStruct{}
 

上面的代码定义了一个名为 `MyStruct` 的结构体,并实现了 `MyInterface` 接口中定义的 `Method1` 和 `Method2` 方法。然后,我们创建了一个类型为 `MyInterface` 的变量 `myVar`,并将它设置为 `MyStruct` 的实例。由于 `MyStruct` 实现了 `MyInterface` 中定义的所有方法,因此它被认为是实现了 `MyInterface` 接口的类型,可以被赋值给 `MyInterface` 类型的变量。

需要注意的是,Go 中的接口是隐式实现的,也就是说,类型不需要显式声明它实现了哪个接口只要实现了接口中定义的所有方法即可。这种灵活性使得 Go 中的接口非常强大和易于使用。

下面是一个用Go 接口实现的工厂模式代码:

package main

import "fmt"

type Product interface {
    GetName() string
}

type ProductA struct {}

func (p *ProductA) GetName() string {
    return "ProductA"
}

type ProductB struct {}

func (p *ProductB) GetName() string {
    return "ProductB"
}

type Factory interface {
    CreateProduct() Product
}

type FactoryA struct {}

func (f *FactoryA) CreateProduct() Product {
    return &ProductA{}
}

type FactoryB struct {}

func (f *FactoryB) CreateProduct() Product {
    return &ProductB{}
}

func main() {
    factoryA := new(FactoryA)
    productA := factoryA.CreateProduct()
    fmt.Println(productA.GetName())

    factoryB := new(FactoryB)
    productB := factoryB.CreateProduct()
    fmt.Println(productB.GetName())
}

在这个示例代码中,我们定义了一个Product接口,它包含一个GetName方法。然后我们定义了两个实现了Product接口的结构体:ProductA和ProductB。接下来,我们定义了一个Factory接口,它包含一个CreateProduct方法返回一个Product实例。我们实现了两个工厂结构体:FactoryA和FactoryB,它们实现了Factory接口并分别返回一个ProductA和ProductB实例。最后在main函数中,我们可以通过调用不同的工厂实例的CreateProduct方法来创建不同的Product实例,并通过GetName方法获取各自的名称。 

2. interface 是一种特殊的类型,它可以表示任何类型的值

在 Golang 中,interface 是一种特殊的类型,它可以表示任何类型的值。使用 interface,我们可以在一个函数或方法中处理各种类型的值,而不需要知道值的具体类型。

要将 interface 类型转换为其具体类型的对象,我们可以使用类型断言 (type assertion)。类型断言的语法如下:


value, ok = expression.(T)
 

其中,`expression` 是一个接口类型的值。`T` 是要将其转换为的具体类型。如果转换成功,那么 `value` 是转换后的值,`ok` 是 true。如果转换失败,那么 `value` 是类型 `T` 的零值,`ok` 是 false。

下面是一个用于将 interface 类型转换为其具体类型的示例代码:

package main

import "fmt"

type Animal interface {
    Sound() string
}

type Dog struct{}

func (d Dog) Sound() string {
    return "Woof!"
}

type Cat struct{}

func (c Cat) Sound() string {
    return "Meow!"
}

func main() {
    var animal Animal

    // create a dog object and assign it to the interface variable
    animal = Dog{}
    dog, ok := animal.(Dog)
    if ok {
        fmt.Println(dog.Sound()) // output: Woof!
    }

    // create a cat object and assign it to the interface variable
    animal = Cat{}
    cat, ok := animal.(Cat)
    if ok {
        fmt.Println(cat.Sound()) // output: Meow!
    }
}


 

在上面的代码中,我们定义了 `Animal` 接口,并为其定义了两种实现:`Dog` 和 `Cat`。在 `main()` 函数中,我们创建了一个名为 `animal` 的 `Animal` 类型的变量,并先后将 `Dog` 和 `Cat` 对象分配给它。然后我们使用类型断言将值转换为 `Dog` 和 `Cat` 类型的变量。 通过此方法,我们可以从接口变量中提取出具体类型并对其进行操作。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田猿笔记

写文章不容易,希望大家小小打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值