Java单文件用于外部类

huangapple go评论95阅读模式
英文:

Java Single File for External Classes

问题

我认为Java的做法是将外部类放在单独的文件中。因此,我最终的目录结构如下所示:

  1. /main/main.java
  2. /main/libs/class01.java
  3. /main/libs/class02.java
  4. /main/libs/class03.java

虽然这样做也可以,但其中一些类真的非常简单,例如,如果所有的类都只是:

  1. package libs;
  2. import java.util.*;
  3. public class class01 {
  4. public Integer idno;
  5. public String name;
  6. public class01() {
  7. idno = 1;
  8. name = "First";
  9. }
  10. }

那么单独的文件似乎有点过多。我想知道是否有一种方法将它们合并到单个文件中,类似于在.NET C#中使用命名空间的方式,如下所示:

  1. using System;
  2. using System.Data;
  3. namespace allClasses {
  4. public class class01 {
  5. public Integer idno;
  6. public String name;
  7. public class01() {
  8. idno = 1;
  9. name = "First";
  10. }
  11. }
  12. public class class02 {
  13. public Integer idno;
  14. public String name;
  15. public class02() {
  16. idno = 2;
  17. name = "Second";
  18. }
  19. }
  20. public class class03 {
  21. public Integer idno;
  22. public String name;
  23. public class03() {
  24. idno = 2;
  25. name = "Third";
  26. }
  27. }
  28. }

我可以在主程序中使用它们,就好像它们是分开的一样:

  1. using System;
  2. using allClasses;
  3. namespace main_module {
  4. class main {
  5. static void Main(string[] args) {
  6. class01 newClass01 = new class01();
  7. class02 newClass02 = new class02();
  8. class03 newClass03 = new class03();
  9. }
  10. }
  11. }

对于比较两者,我感到抱歉,只是因为.NET C#是我能够展示我想要实现的内容的最佳示例。我还不知道如何在Java中做到这一点,非常感谢您提供指导。

英文:

I believe the Java way is to keep external classes in separate files. So I've ended up with a directory structure like the following

  1. /main/main.java
  2. /main/libs/class01.java
  3. /main/libs/class02.java
  4. /main/libs/class03.java

Which would be fine but some of the classes are really puny little things, for example if all the classes were just

  1. package libs;
  2. import java.util.*;
  3. public class class01 {
  4. public Integer idno;
  5. public String name;
  6. public class01() {}
  7. idno = 1;
  8. name = "First";
  9. }
  10. }

Then separate files seem like they could get a bit excessive. I'm wondering if there is a way to combine them into a single file similar to the way that you can do in .Net C# with namespaces like the following

  1. using System;
  2. using System.Data;
  3. namespace allClasses {
  4. public class class01 {
  5. public Integer idno;
  6. public String name;
  7. public class01() {}
  8. idno = 1;
  9. name = "First";
  10. }
  11. }
  12. public class class02 {
  13. public Integer idno;
  14. public String name;
  15. public class02() {}
  16. idno = 2;
  17. name = "Second";
  18. }
  19. }
  20. public class class03 {
  21. public Integer idno;
  22. public String name;
  23. public class03() {}
  24. idno = 2;
  25. name = "Third";
  26. }
  27. }
  28. }

Which I can use in my main as if they were all separate

  1. using System;
  2. using allClasses;
  3. namespace main_module {
  4. class main {
  5. static void Main(string[] args) {
  6. class01 newClass01 = new class01();
  7. class02 newClass02 = new class02();
  8. class03 newClass03 = new class03();
  9. }
  10. }
  11. }

I'm sorry for comparing the two it's just that .Net C# is the best example I can show of what I am trying to achieve. I don't know a way of doing this yet in Java, some guidance would be very much appreciated

答案1

得分: 1

你可以将它们声明为另一个公共类内部的 public static class
例如:

  1. public class Util {
  2. public static class A {}
  3. public static class B {}
  4. }

然后在主类中可以这样引用它们:

  1. new Util.A();

这样可以将源代码合并到一个单独的类中,但正如 @oleg-cherednik 提到的,编译后将会生成几个独立的类文件。

英文:

You can declare them as public static class within another public class.
E.g.

  1. public class Util {
  2. public static class A {}
  3. public static class B {}
  4. }

Then in your main class you can reference them as:

  1. new Util.A();

This will help you combine source code into a single class, but as @oleg-cherednik mentioned, when compiled there will be several separate class files.

huangapple
  • 本文由 发表于 2020年9月13日 18:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63869805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定