英文:
Concrete type vs Not concrete type in Golang
问题
具体类型 vs 非具体类型。
在Golang中,具体类型和非具体类型之间有什么明显的区别,你如何决定使用哪种类型?以下是一个示例代码:
type Animal1 interface {
speak()
}
type Animal2 interface {
speak() string
}
- 在Animal1接口的方法
speak()
中缺少类型,这是否使它成为非具体类型? - 你如何决定何时使用具体类型或非具体类型?
英文:
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
}
- Does the absence of type in Animal1 interface method
speak()
makes it a not concrete type? - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论