java 部分类/部分类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2764234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:42:58  来源:igfitidea点击:

partial classes/partial class file

javapartial-classes

提问by Ravisha

In C# .net there is a provision to have two different class files and make them a single class using the keyword partial keyword.this helps it to keep [for ex]UI and logic seperate. of course we can have two classes to achieve this one for UI and other for logic. Can this be achieved in java some how?

在 C# .net 中,提供了两个不同的类文件,并使用关键字 partial 关键字使它们成为单个类。这有助于它保持 [例如] UI 和逻辑分离。当然,我们可以有两个类来实现这一点,一个用于 UI,另一个用于逻辑。这可以在java中实现吗?

回答by polygenelubricants

On source file splitting

关于源文件拆分

No. Java source codes can not be split across multiple files.

不可以。Java 源代码不能跨多个文件拆分。

From the Wikipedia article Comparison of Java and C Sharp

来自维基百科文章Java 和 C Sharp 的比较

The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows a class definition to be split into several files, by using the partial keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related.

Sun Microsystems Java 编译器要求源文件名必须与其中唯一的公共类匹配,而 C# 允许同一文件中有多个公共类,并且对文件名没有限制。C# 2.0 及更高版本允许通过在源代码中使用 partial 关键字将类定义拆分为多个文件。在 Java 中,公共类将始终位于其自己的源文件中。在 C# 中,源代码文件和逻辑单元分离并不紧密相关。



On separating logic and UI into their own classes

关于将逻辑和 UI 分离到它们自己的类中

The two classes approach is a muchbetter solution than the one-class-two-source approach in this case, because the separation is enforced in the design, not just physical separation in the source codes.

两个类的方法是一个比在这种情况下,一类双源的方法更好的解决方案,因为分离是在设计执行,在源代码中不只是物理分离。

See also

也可以看看