英文:
Does Go have Partial Classes?
问题
Go语言中有部分类吗?
就像C#中的这个例子一样?
http://msdn.microsoft.com/en-us/library/wa80x488.aspx
我猜应该没有,因为你不能部分声明结构体。
英文:
Are there partial classes in Go?
Like the one here in C#?
http://msdn.microsoft.com/en-us/library/wa80x488.aspx
I suppose there isn't any as you cannot partially declare structs.
答案1
得分: 5
类型的方法声明不需要与类型声明或其他类型的方法声明位于同一个源文件中。方法声明需要位于与类型声明相同的包中。
类型声明不能跨文件分割。
Go语言没有类。
英文:
The method declarations for a type do not need to be in the same source file as the type declaration or other method declarations for the type. The method declarations do need to be in the same package as the type declaration.
A type declaration cannot be split across files.
Go does not have classes.
答案2
得分: 0
在Go语言中,你可以在同一个包的任何文件中为任何类型定义关联的方法。以下是一个关于对象foo
和函数Bar
的小例子:
package main
import "fmt"
type foo struct {} // 在这里放置与类型相关的变量
func (/*f*/ foo) Bar() { // 如果需要访问对象中的任何元素,在foo前面放置一个值
// 做一些有趣的事情
fmt.Println("方法被调用 :D")
}
func main() {
example := foo{}
example.Bar()
}
只要foo
和bar
的声明出现在同一个包中,它们可以放置在任何文件中。
希望这个例子展示了你想要实现的功能,以及Go是否支持。
英文:
In Go you can have a method associated with any type within the same package in any file. Take this small example of object foo
with function Bar
.
package main
import "fmt"
type foo struct {} // put variables associated with the type here
func ( /*f*/ foo) Bar() { // put a value in front of foo if u need to access any elements in the object
// do something interesting
fmt.Println("Method called :D")
}
func main() {
example := foo{}
example.Bar()
}
As long as foo
and bar
declarations occur in the same package they can be placed in any file.
I hope this demonstrated the desired functionality you are trying to implement / wondering if Go can support.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论