java 为什么每个文件只有一个类

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

Why only one class per file

java

提问by Espen

Still coming from C++ I find it cumbersome to create many small helper classes and objects. Mostly because I have to have one file per class. I basically have a class like this:

仍然来自 C++ 我发现创建许多小的帮助类和对象很麻烦。主要是因为我每个班级必须有一个文件。我基本上有一个这样的课程:

public class mySimpleClass {
    public float[] numbers = new float[ 4 ];
}

And then I have this class:

然后我有这门课:

public class myNotSoSimpleClass {
    private mySimpleClass;
    .
    .
    .
}

The second class which is not so simple is ok to have in its own file. However, the simple class is connected to the not so simple class and it would be very nice to not have to have those few lines of code in its own file.

不那么简单的第二个类可以放在它自己的文件中。然而,简单的类与不那么简单的类相连,如果它自己的文件中不必包含那几行代码,那就太好了。

So to sum it up, this is what one could do in C++:

总而言之,这就是在 C++ 中可以做的事情:

public class myNotSoSimpleClass {
    private struct mySimpleClass {
        float numbers[ 4 ];
    } myStruct;
    .
    .
    .
}

Is it possible to embed/use one class inside another class, or the same file? I would just find it easier to work with large projects if I could set up these two classes into one file. Or is Java a strictly one class per file, and that's it, language?

是否可以在另一个类或同一个文件中嵌入/使用一个类?如果我可以将这两个类设置到一个文件中,我会发现处理大型项目会更容易。或者 Java 是一个严格的每个文件一个类,就是这样,语言?

采纳答案by Dave McClelland

It is possible to have one class inside of another class. In Java, it's called an inner class

可以在另一个类中包含一个类。在 Java 中,它被称为内部类

回答by Mark Bell

You can have multiple classes in the same file, but only one of them can be public:

您可以在同一个文件中包含多个类,但只能是其中之一public

Java: Multiple class declarations in one file

Java:一个文件中的多个类声明

回答by Jay

As several others have pointed out, you can have any number of non-public classes in a single file.

正如其他几个人指出的那样,您可以在单个文件中包含任意数量的非公共类。

To answer the "why", one likely factor is that it makes your classes easy to find. If I see a reference in a program to class Foobar, I immediately know that it must either be in the same file with the current class, or it must be in a file named Foobar.java. I can check the imports to see what directory it is in.

要回答“为什么”,一个可能的因素是它使您的课程易于查找。如果我在程序中看到对 Foobar 类的引用,我立即知道它必须与当前类位于同一文件中,或者必须位于名为 Foobar.java 的文件中。我可以检查导入以查看它在哪个目录中。

When I work on C or C++ programs, I regularly find myself having to do Windows file searches or Linux grep's to find the source for a class.

当我处理 C 或 C++ 程序时,我经常发现自己必须执行 Windows 文件搜索或 Linux grep 来查找类的源代码。

When I write very small programs, I often put all the classes in one file. But if it's going to be more than a few thousand lines, it's much more manageable to break it out.

当我编写非常小的程序时,我经常将所有类放在一个文件中。但是,如果要超过几千行,则将其分解会容易得多。

When I have a class that is only used by one other class, I'll often put it in the same file. Usually this means a class that just exists to temporarily hold some small bundle of data, like I need an array or list of something that requires more than one field.

当我有一个只被其他类使用的类时,我通常会将它放在同一个文件中。通常这意味着一个类只是为了临时保存一些小数据包而存在,比如我需要一个数组或需要多个字段的列表。

回答by Sinjo

The restriction is that you can only have one top level public class per file (you can have nested public classes though). As people have mentioned you can have inner/nested classes within that class.

限制是每个文件只能有一个顶级公共类(尽管您可以嵌套公共类)。正如人们所提到的,您可以在该类中拥有内部/嵌套类。

Another thing which hasn't been mentioned is that you can have another top level class in the same file, as long as it is package-private (visible only within the package). A package-private class is one which is declared without an access modifier.

另一件未提及的事情是,您可以在同一个文件中拥有另一个顶级类,只要它是包私有的(仅在包内可见)。包私有类是在没有访问修饰符的情况下声明的类。

Here's a chapter from Effective Java (which is worth a read) on this subject. http://books.google.co.uk/books?id=ka2VUBqHiWkC&lpg=PA67&ots=yXKoLnv2TX&dq=Minimize%20the%20accessibility%20of%20classes%20and%20members&pg=PA67#v=onepage&q&f=false

这是 Effective Java 中关于这个主题的一章(值得一读)。http://books.google.co.uk/books?id=ka2VUBqHiWkC&lpg=PA67&ots=yXKoLnv2TX&dq=Minimize%20the%20accessibility%20of%20classes%20and%20members&pg=PA67#v=onepage&q&f=

回答by Plínio Pantale?o

The rule is one top-level public class per file. Inner class, private class and some other stuff are allowed

规则是每个文件一个顶级公共类。允许内部类、私有类和其他一些东西

回答by wheaties

Java allows you to create inner classes. That is, classes within another class. That's where you should put your helper classes. On the other hand, I feel your pain. Being primarily a C++ coder myself Java can sometimes feel like it forces boilerplate upon you.

Java 允许您创建内部类。也就是说,另一个类中的类。那就是您应该放置助手类的地方。另一方面,我感受到你的痛苦。我自己主要是一名 C++ 编码员,Java 有时会觉得它强迫你使用样板。

回答by Andy Thomas

Yes, you can nest a class inside another.

是的,您可以将一个类嵌套在另一个类中。

Contrary to many of the answers here, there are no restrictions on public visibility.

与这里的许多答案相反,公众知名度没有限制。

Nested classes include:

嵌套类包括:

  • Static nested classes.
  • Inner classes, which have an implicit pointer to an instance of the containing class, and are allowed to access its private parts.
  • Anonymous inner classes, which can be created within methods of the containing class, without a name. These are convenient, for example, for short event listeners.
  • 静态嵌套类。
  • 内部类,具有指向包含类的实例的隐式指针,并允许访问其私有部分。
  • 匿名内部类,可以在包含类的方法中创建,没有名称。例如,这些对于短事件侦听器很方便。

More details are available in this Oracle tutorial.

Oracle 教程中提供了更多详细信息

回答by nosirrahcd

In Java, there is no way to put two publictop-level classes into the same file, but this restriction does not apply to private/inner classes. So you could have something like:

在 Java 中,没有办法将两个公共顶级类放在同一个文件中,但是这个限制不适用于私有/内部类。所以你可以有类似的东西:

public class MyNotSoSimpleClass {
    private MySimpleClass mySimpleClass = new MySimpleClass();

    private class MySimpleClass {
        public float numbers[4];
        ...
    }
....

}

}

回答by dirbacke

In Java there is something called nested classes, read more about it here!

在 Java 中有一种叫做嵌套类的东西,在这里阅读更多关于它的信息