java 在Java中,我们可以将一个类分成多个文件吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5309260/
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
In Java, can we divide a class into multiple files
提问by user496949
Any possibility to divide a class into multiple physical files using Java?
是否有可能使用 Java 将一个类划分为多个物理文件?
采纳答案by Jon Skeet
No, the whole of a class has to be in a single file in Java.
不,整个类必须在 Java 中的单个文件中。
If you're thinking of C#'s "partial types" feature, there's no equivalent in Java. (If you weren't thinking of C#, ignore this :)
如果您正在考虑 C# 的“部分类型”功能,那么 Java 中没有等效项。(如果您没有想到 C#,请忽略此 :)
回答by spa
This might be a good idea if the class is really so large such that the implemented concepts are not easy to grasp. I see two different ways to do this:
如果类真的很大,以至于实现的概念不容易掌握,这可能是一个好主意。我看到两种不同的方法来做到这一点:
Use inheritance:Move general concepts of the class to a base class and derive a specialized class from it.
Use aggregation:Move parts of your class to a separate class and establish a relationship to the second class using a reference.
使用继承:将类的一般概念移至基类,并从中派生出专门的类。
使用聚合:将类的一部分移到一个单独的类中,并使用引用建立与第二个类的关系。
As previously mentioned, there is no concept like partial classes in Java, so you really have to use these OOP mechanisms.
如前所述,Java 中没有像部分类这样的概念,因此您确实必须使用这些 OOP 机制。
回答by Graf von und zu Nix
Yes You Can!
是的你可以!
For the sake of completion:
为了完成:
Since Java 8, you have the concept of default methods.
从 Java 8 开始,您就有了默认方法的概念。
you can split up your class into multiple files/subclasses by gently abusing interfaces
你可以轻轻类分成多个文件/子AB使用接口
observe:
观察:
MyClassPartA.java
MyClassPartA.java
interface MyClassPartA{
public default int myMethodA(){return 1;}
}
MyClassPartB.java
MyClassPartB.java
interface MyClassPartB{
public default String myMethodB(){return "B";}
}
and combine them:
并将它们组合起来:
MyClass.java
我的类
public class MyClass implements MyClassPartA, MyClassPartB{}
and use them:
并使用它们:
MyClass myClass = new MyClass();
System.out.println(myClass.myMethodA());
System.out.println(myClass.myMethodB());
You can even pass variables between classes/files with abstract getters and setters that you will need to realize/override in the main class, or a superclass of that however.
您甚至可以使用抽象的 getter 和 setter 在类/文件之间传递变量,您需要在主类或超类中实现/覆盖这些变量。
回答by sleske
Using just javac
, this is not possible. You could of course combine multiple files into a single .java file as part of your build process, and invoke javac
afterwards, but that would be cumbersome on so many levels that it is unlikely to be useful.
仅使用javac
,这是不可能的。您当然可以将多个文件合并为一个 .java 文件作为构建过程的一部分,然后再调用javac
,但这在很多层面上都很麻烦,不太可能有用。
Maybe you could explain your problem, then we can help better.
也许您可以解释您的问题,然后我们可以更好地提供帮助。
If you feel your .java files are too large, you should probably consider refactoring.
如果您觉得您的 .java 文件太大,您可能应该考虑重构。
回答by biziclop
Of course it is possible, but I don't think it's useful at all.
当然有可能,但我认为它根本没有用。
To start off, divide isn't really the question I guess, you just compile the file and split it up whichever way you want.
首先,我想划分并不是真正的问题,您只需编译文件并以您想要的任何方式拆分它。
Now to put them back together all you need to do is to write a custom class loader which loads all the pieces, combines them into a single byte array, then calls defineClass()
.
现在要将它们重新组合在一起,您需要做的就是编写一个自定义类加载器来加载所有部分,将它们组合成一个字节数组,然后调用defineClass()
.
Like I said, it does look pretty pointless and is probably not what you want and definitely not what you need, but it is technically possible.
就像我说的那样,它看起来确实毫无意义,可能不是您想要的,也绝对不是您需要的,但在技术上是可行的。
(I did something similar once as a joking way of obfuscating code: bytes of the class file were scattered in constants of all the other classes in the application. It was fun, I have to admit.)
(我曾经做过类似的事情,以一种开玩笑的方式混淆代码:类文件的字节分散在应用程序中所有其他类的常量中。这很有趣,我不得不承认。)
回答by Peter Knego
No, in Java this can not be done.
不,在 Java 中这是无法做到的。
回答by byyyk
No you can't. If your class is too big than you should split it into two or more.
不,你不能。如果你的班级太大,你应该把它分成两个或更多。