简体   繁体   English

在同一文件中声明新类是好的“形式”吗?

[英]is it good “form” to declare new classes in the same file?

I code in Python a lot, and I frequently create classes. 我经常使用Python编写代码,并且经常创建类。 Now, I'm not sure if this is good Python form, but I just declare a class in the same file as my main(). 现在,我不确定这是否是良好的Python形式,但我只是在与main()相同的文件中声明一个类。

class foo {
...
} 

I'm wondering if it's good form in Java to do the same? 我想知道这样做是否是Java的好形式?

For example, 例如,

class foo {
    public static int name;
    public static int numPoints;
    public static int[] points;
}

public class bar {

    public static void main(String[] args) {
    ...
    }
}

Does not throw errors in Eclipse, so it must be allowed. 在Eclipse中不会引发错误,因此必须允许。 But is it okay to do? 但是可以吗? Would it be better to just declare this class in a separate file..? 最好在单独的文件中声明此类。

Edit: I just want to emphasize that my new class literally is just a container to hold the same type of data multiple times, and literally will only have like 3 values. 编辑:我只是想强调一下,我的新类从字面上看只是一个容器,用于多次保存相同类型的数据,并且从字面上看只有3个值。 So it's total about 5 lines of code. 因此,总共约5行代码。 The question is - does this merit a new file? 问题是-这值得一个新文件吗?

Depends on what you want the class to do. 取决于您要在课堂上做什么。 If it's only used by the class with main method, there's little loss in declaring it in the same file, although I'd prefer to declare it as a private static inner class then, for clarity and less namespace clutter: 如果仅由具有main方法的类使用它,则在同一文件中声明它几乎没有什么损失,尽管我更喜欢将其声明为private static内部类,以保持清晰度和减少命名空间的混乱:

class Program {
    public static void main(String[] args){ ... }
    private static class Foo {
        public int name;
        public int numPoints;
        public int[] points;
    }
}

If >1 other class use your class, it is generally considered good form to have it in a file of its own. 如果> 1个其他类使用您的类,通常将其保存在自己的文件中是一种很好的形式。 If it's declared as a public class , it must be in a separate file with the same name as the class. 如果将其声明为public class ,则它必须位于与该类同名的单独文件中。 (Unless its an inner class, of course. :) (当然,除非是内部类。

Edit: I just want to emphasize that my new class literally is just a container to hold the same type of data multiple times, and literally will only have like 3 values. 编辑:我只是想强调一下,我的新类从字面上看只是一个容器,用于多次保存相同类型的数据,并且从字面上看只有3个值。 So it's total about 5 lines of code. 因此,总共约5行代码。 The question is - does this merit a new file? 问题是-这值得一个新文件吗?

I think, code size should not be a criteria to use a separate file. 我认为,代码大小不应成为使用单独文件的标准。

From a maintenance point of view it is always easier if each class is declared in its own file (with the filename the same as the classname). 从维护的角度来看,如果每个类都在自己的文件中声明(文件名与类名相同),则总是比较容易。

I do not consider what you are proposing as good form, no. 我不认为您的提议是好的形式,不。

If You write a "use-once-and-forget" program it is Ok, but it is technically impossible for real applications. 如果您编写了“使用一次忘记”程序,则可以,但是从技术上讲,对于实际应用程序是不可能的。

Classes should be declared in their own files, because each file has to correspond to one public class (that is the idea of Java Developers). 应该在自己的文件中声明类,因为每个文件必须对应一个公共类(这是Java Developers的思想)。 There can be only one public class per file, so if You need to use foo class in other class (outside this file), You have to split them into separate files. 每个文件只能有一个公共类,因此,如果需要在其他类(此文件之外)中使用foo类,则必须将它们拆分为单独的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM