简体   繁体   English

有没有办法在 Java 中执行部分类(如 C#)?

[英]Is there a way to do partial classes in Java (like C#)?

C# has this great concept where a class can be spread across multiple .cs files. C# 有一个很棒的概念,其中一个类可以分布在多个 .cs 文件中。 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.然后,您可以通过源文件中的功能将此代码展开。

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).更新:好的,一开始我告诉自己这必须是一个大类(它执行 DOCX 文件内容的布局)。 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.但是在发布这个之后,我想了更多,它真的让我感到困扰,这是一个大的(目前有 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.因此,我考虑了一些替代方案,并想出了一种将其分解为一个主类和大约 20 个辅助类的好方法。 It works very well this way really separating out the functionality into each part.这种方式非常有效,真正将功能分离到每个部分。

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.) (这与最初的问题无关,但我认为值得分享。)

No. Java does not support partial classes.不。Java 不支持部分类。 You'll find more in depth discussion in this question and this question .你会在这个问题这个问题中找到更深入的讨论。

No, Java doesn't support partial classes.不,Java 不支持部分类。

If this is just idle curiosity, check out Scala .如果这只是无聊的好奇心,请查看Scala It compiles to .class files just like Java, and has full interop.它像 Java 一样编译为 .class 文件,并且具有完整的互操作性。 It supports a rough equivalent of partial classes as "traits".它支持部分类的粗略等价物作为“特征”。

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

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 访问原始类的公共方法。

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.相反,您可以在该文件和(顶级)类定义中定义其他类。

No, it is one of the wonderful features in Java.不,它是 Java 中的一项精彩功能。 A Class must be identical with her filename. Class 必须与她的文件名相同。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM