Java单文件用于外部类

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

Java Single File for External Classes

问题

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

/main/main.java
/main/libs/class01.java
/main/libs/class02.java
/main/libs/class03.java

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

package libs;
import java.util.*;

public class class01 {
    public Integer idno;
    public String name;

    public class01() {
        idno = 1;
        name = "First";
    }
}

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

using System;
using System.Data;

namespace allClasses {

    public class class01 {

        public Integer idno;
        public String name;

        public class01() {
            idno = 1;
            name = "First";
        }
    }

    public class class02 {

        public Integer idno;
        public String name;

        public class02() {
            idno = 2;
            name = "Second";
        }
    }

    public class class03 {

        public Integer idno;
        public String name;

        public class03() {
            idno = 2;
            name = "Third";
        }
    }
}

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

using System;

using allClasses;

namespace main_module {

    class main {

        static void Main(string[] args) {

            class01 newClass01 = new class01();
            class02 newClass02 = new class02();
            class03 newClass03 = new class03();
        }
    }
}

对于比较两者,我感到抱歉,只是因为.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

/main/main.java
/main/libs/class01.java
/main/libs/class02.java
/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

package libs;
import java.util.*;
public class class01 {

    public Integer idno;
    public String name;

    public class01() {}
        idno = 1;
        name = "First";
    }

}

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

using System;
using System.Data;

namespace allClasses {

    public class class01 {

        public Integer idno;
        public String name;

        public class01() {}
            idno = 1;
            name = "First";
        }

    }

    public class class02 {

        public Integer idno;
        public String name;

        public class02() {}
            idno = 2;
            name = "Second";
        }

    }

    public class class03 {

        public Integer idno;
        public String name;

        public class03() {}
            idno = 2;
            name = "Third";
        }

    }

}

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

using System;

using allClasses;

namespace main_module {

    class main {

        static void Main(string[] args) {

            class01 newClass01 = new class01();
            class02 newClass02 = new class02();
            class03 newClass03 = new class03();

        }

    }

}

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
例如:

public class Util {
  public static class A {}
  public static class B {}
}

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

new Util.A();

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

英文:

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

public class Util {
  public static class A {}
  public static class B {}
}

Then in your main class you can reference them as:

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:

确定