Concrete type vs Not concrete type in Golang

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

Concrete type vs Not concrete type in Golang

问题

具体类型 vs 非具体类型。
在Golang中,具体类型和非具体类型之间有什么明显的区别,你如何决定使用哪种类型?以下是一个示例代码:

type Animal1 interface {
    speak()
}

type Animal2 interface {
    speak() string
}
  1. 在Animal1接口的方法speak()中缺少类型,这是否使它成为非具体类型?
  2. 你如何决定何时使用具体类型或非具体类型?
英文:

Concrete type vs Not concrete type.
What is the distinct difference between the concrete type and not concrete type in Golang and how do you decide which one to use ?
here's a sample code:

type Animal1 interface{
    speak()
}

type Animal2 interface{
    speak() string
}
  1. Does the absence of type in Animal1 interface method speak() makes it a not concrete type?
  2. How do you decide when to use either concrete or none concrete type ?

答案1

得分: 3

一个具体类型是一个不是接口的数据类型。在你的例子中,两个类型都是接口。第一个接口有一个不返回任何内容的speak方法。第二个接口有一个返回字符串的speak方法。

英文:

A concrete type is a data type that is not an interface. In your example, both types are interfaces. The first one has a speak method that does not return anything. The second one has a speak method that returns a string.

huangapple
  • 本文由 发表于 2023年2月22日 02:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75524733.html
匿名

发表评论

匿名网友

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

确定