理解冲突的接口

huangapple go评论86阅读模式
英文:

Understanding conflicting Interfaces

问题

我正在尝试理解接口的概念。
以下是我的测试代码:

package main

import "fmt"

// 接口A
type InterfaceA interface {
	A() string
}

// 接口B
type InterfaceB interface {
	A() string
}

// 用户定义的结构体
type structure struct {
	a string
}

// 实现A()方法,但是是实现哪个A()方法呢?
func (strt structure) A() string {
	return strt.a
}

func main() {
	fmt.Println("开始")
	variable := structure{"hello"}
	fmt.Println(variable.A())
}

根据文档,我理解在Go语言中没有像其他语言那样明确提到"实现"(Implements)的概念。但是当我调用variable.A()时,我的类型structure使用的是哪个接口?是InterfaceA还是InterfaceB?此外,我是否正确地实现了接口?

英文:

I am trying to understand the interface concept.
The following is my test code:

package main

import "fmt"

//interface
type InterfaceA interface {
	A() string
}

//interface
type InterfaceB interface {
	A() string
}

//user defined type structure
type structure struct {
	a string
}

//implementing A(), but which A() ?
func (strt structure) A() string {
	return strt.a
}

func main() {
	fmt.Println("Start")
	variable := structure{"hello"}
	fmt.Println(variable.A())
}

As per the documentation, I understand that there is no explicit mention of "Implements" like in other languages. But when I call variable.A() which interface is my type structure using? InterfaceA or InterfaceB ? Plus, am I actually implementing the interface properly?

答案1

得分: 3

当你调用variable.A()时,你没有使用任何接口。你是在调用类型structure的方法。

你可以使用接口来调用相同的方法:

variable := structure{"hello"}
var ia InterfaceA
ia = variable
ia.A() // 这将调用 variable.A
var ib InterfaceB
ib = variable
ib.A() // 这也将调用 variable.A
英文:

When you call variable.A(), you are not using any interfaces. You are calling a method of the type structure.

You can call the same method using an interface:

variable := structure{"hello"}
var ia InterfaceA
ia=variable
ia.A() // This calls variable.A
var ib InterfaceB
ib=variable
ib.A() // This also calls variable.A


</details>



# 答案2
**得分**: 1

你的接口之间没有冲突;接口之间不会发生冲突。它们可以彼此冗余,因为它们描述的是同一件事情,但这并不会产生冲突或其他实质性的问题。

至于`structure`使用哪个接口:都不使用。结构体等具体类型不使用接口,它们实现接口。实现接口是隐式和自动的。这只是一个事实陈述,而不是一个机制(除非你明确将其用作机制)。此外,在你的程序中,两个接口都没有被广泛使用。你的`variable`是一个`structure`,并且被用作一个`structure`。它*可以*在不同的时间被用作`InterfaceA`或`InterfaceB`,或者同时用作两者,但在这个例子中你没有这样做。

请参考Burak的答案,了解如何通过接口使用`structure`值。

<details>
<summary>英文:</summary>

Your interfaces do not conflict with each other; interfaces do not conflict. They can be redundant with each other as they describe the same thing, but that doesn&#39;t create a conflict nor any other tangible problem.

As for which interface is `structure` using: neither. Concrete types such as structs don&#39;t use interfaces, they implement them. Implementing of an interface is implicit and automatic. It&#39;s just a statement of fact, not a mechanism (until you explicitly use it as such). Further, neither interface is used to any extent in your program. Your `variable` is a `structure` and is being used as a `structure`. It *could* be used as *either* `InterfaceA` or `InterfaceB`, or both of them at different times, but you are doing no such thing in this example.

See Burak&#39;s answer for how you *could* use the `structure` value through the interfaces.

</details>



huangapple
  • 本文由 发表于 2022年2月9日 03:35:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71039878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定