有没有办法在 Java 中执行部分类(如 C#)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20577352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to do partial classes in Java (like C#)?
提问by David Thielen
C# has this great concept where a class can be spread across multiple .cs files. This works great where you want a single object (member variables that all the code needs) but there's a ton of code. You can then spread this code out by functionality across source files.
C# 有一个很棒的概念,其中一个类可以分布在多个 .cs 文件中。这在您需要单个对象(所有代码都需要的成员变量)但有大量代码的情况下非常有用。然后,您可以通过源文件中的功能将此代码展开。
Is there a way to do this in Java?
有没有办法在 Java 中做到这一点?
Update:Ok at first I told myself that this must be a single large class (it performs layout of the content of a DOCX file). But then after posting this I thought about it more and it really bothered me that it is this one large (5,000+ lines at present) class.
更新:好的,一开始我告诉自己这必须是一个大类(它执行 DOCX 文件内容的布局)。但是在发布这个之后,我想了更多,它真的让我感到困扰,这是一个大的(目前有 5,000 多行)类。
So I thought through some alternatives and came up with a good way to break it out into one main class and about 20 helper classes. It works very well this way really separating out the functionality into each part.
因此,我考虑了一些替代方案,并想出了一种将其分解为一个主类和大约 20 个辅助类的好方法。这种方式非常有效,真正将功能分离到每个部分。
So... while I think partial classes is a useful construct at times, in this case the lack of partial classes caused me to come up with a better design. (And this has nothing to do with the initial question, but I thought it was worth sharing.)
所以...虽然我认为部分类有时是一个有用的构造,但在这种情况下,缺少部分类使我想出了更好的设计。(这与最初的问题无关,但我认为值得分享。)
回答by Asaph
No. Java does not support partial classes. You'll find more in depth discussion in this questionand this question.
回答by PNS
There is no way to have partial class definitions, spread across files. Every class must be defined in its own namesake file.
没有办法让部分类定义分布在文件中。每个类都必须在其自己的同名文件中定义。
On the contrary, you can define additional classes within that file and within the (top level) class definition.
相反,您可以在该文件和(顶级)类定义中定义其他类。
回答by lostinexceptions
No, it is one of the wonderful features in Java. A Class must be identical with her filename. :-)
不,它是 Java 中很棒的特性之一。Class 必须与她的文件名相同。:-)
回答by Dylan
No, Java doesn't support partial classes.
不,Java 不支持部分类。
If this is just idle curiosity, check out Scala. It compiles to .class files just like Java, and has full interop. It supports a rough equivalent of partial classes as "traits".
如果这只是无聊的好奇心,请查看Scala。它像 Java 一样编译为 .class 文件,并且具有完整的互操作性。它支持部分类的粗略等价物作为“特征”。
An example of trait usage:
trait 用法的一个例子:
trait FunctionalityA {
// some stuff implemented here
}
trait FunctionalityB {
// more stuff implemented...
}
// etc...
class MyBigClass extends FunctionalityA with FunctionalityB with FunctionalityC
回答by chrvip
I find a way to emulate C# partial class depend on @Delegate in lombok
我找到了一种模拟 C# 部分类的方法依赖于 lombok 中的 @Delegate
@Getter
@Setter
public class User {
String name;
@Delegate
private UserDelegate delegate= new UserDelegate(this);
}
public class UserDelegate {
private User expandOwner;
public UserDelegate(User expandOwner) {
this.expandOwner = expandOwner;
}
public void doSomethinga() {
System.out.println(expandOwner.getName() + "did something!");
}
}
// how to use:
User user= new User();
user.setName("Chrisme");
user.doSomethinga();
you can access original class's public method via expandOwner in delegate class.
您可以通过委托类中的 expandOwner 访问原始类的公共方法。