一个java文件可以有多个类吗?

[英]Can a java file have more than one class?


What is the purpose of having more than one class in a Java file ? I am new to Java.

在Java文件中拥有多个类的目的是什么?我是Java新手。

Edited: That can be achieved by creating a inner class inside a public class, right?

编辑:这可以通过在公共类中创建内部类来实现,对吧?

17 个解决方案

#1


117  

Yes, it can. However, there can only be one public top-level class per .java file, and public top-level classes must have the same name as the source file.

是的,它可以。但是,每个.java文件只能有一个公共顶级类,公共顶级类必须与源文件具有相同的名称。

The purpose of including multiple classes in one source file is to bundle related support functionality (internal data structures, support classes, etc) together with the main public class. Note that it is always OK not to do this--the only effect is on the readability (or not) of your code.

在一个源文件中包含多个类的目的是将相关的支持功能(内部数据结构,支持类等)与主公共类捆绑在一起。请注意,不执行此操作总是可以的 - 唯一的影响是代码的可读性(或不可读性)。

#2


15  

If you want to implement a public class, you must implement it in a file with the same name as that class. A single file can contain one public and optionally some private classes. This is useful if the classes are only used internally by the public class. Additionally the public class can also contain inner classes.

如果要实现公共类,则必须在与该类同名的文件中实现它。单个文件可以包含一个公共类和一些私有类。如果类仅由公共类在内部使用,则此选项很有用。此外,公共类还可以包含内部类。

Although it is fine to have one or more private classes in a single source file, I would say that is more readable to use inner and anonymous classes instead. For example one can use an anonymous class to define a Comparator class inside a public class:

虽然在单个源文件中有一个或多个私有类是可以的,但我认为使用内部和匿名类更具可读性。例如,可以使用匿名类在公共类中定义Comparator类:

  public static Comparator MyComparator = new Comparator() {
    public int compare(Object obj, Object anotherObj) {

    }
  };

The Comparator class will normally require a separate file in order to be public. This way it is bundled with the class that uses it.

Comparator类通常需要单独的文件才能公开。这样它就与使用它的类捆绑在一起。

#3


10  

Yes, as many as you want!

是的,尽可能多的人!

BUT, only one "public" class in every file.

但是,每个文件中只有一个“公共”类。

#4


5  

A .java file is called a compilation unit. Each compilation unit may contain any number of top-level classes and interfaces. If there are no public top-level types then the compilation unit can be named anything.

.java文件称为编译单元。每个编译单元可以包含任意数量的顶级类和接口。如果没有公共顶级类型,则编译单元可以命名为任何名称。

//Multiple.java
//preceding package and import statements

class MyClass{...}
interface Service{...}
...
//No public classes or interfaces
...

There can be only one public class/interface in a compilation unit. The c.u. must be named exactly as this public top-level type.

编译单元中只能有一个公共类/接口。 c.u.必须完全按照此公共顶级类型命名。

//Test.java
//named exactly as the public class Test
public class Test{...}
//!public class Operations{...}
interface Selector{...}
...
//Other non-public classes/interfaces

Important points about the main method - part 1

关于主要方法的要点 - 第1部分

Part 2

第2部分

(Points regarding the number of classes and their access levels covered in part 2)

(关于第2部分中课程数量及其访问级别的要点)

#5


4  

Besides anonymous inner classes, another use is private inner classes that implement a public interface (see this article). The outer class can access all private fields and methods of the inner class.

除了匿名内部类之外,另一个用途是实现公共接口的私有内部类(参见本文)。外部类可以访问内部类的所有私有字段和方法。

This lets you create two tightly-coupled classes, such as a model and its view, without exposing the implementations of either. Another example is a collection and its iterators.

这使您可以创建两个紧密耦合的类,例如模型及其视图,而不会暴露其中任何一个的实现。另一个例子是一个集合及其迭代器。

#6


4  

In general, there should be one class per file. If you organise things that way, then when you search for a class, you know you only need to search for the file with that name.

通常,每个文件应该有一个类。如果以这种方式组织,那么当您搜索类时,您知道只需要搜索具有该名称的文件。

The exception is when a class is best implemented using one or more small helper classes. Usually, the code is easiest to follow when those classes are present in the same file. For instance, you might need a small 'tuple' wrapper class to pass some data between method calls. Another example are 'task' classes implementing Runnable or Callable. They may be so small that they are best combined with the parent class creating and calling them.

例外情况是使用一个或多个小帮助程序类最好地实现类。通常,当这些类存在于同一文件中时,代码最容易遵循。例如,您可能需要一个小的“元组”包装类来在方法调用之间传递一些数据。另一个例子是实现Runnable或Callable的'task'类。它们可能非常小,最好与父类创建和调用它们。

#7


4  

Yes you can create more than one public class, but it has to be a nested class.

是的,您可以创建多个公共类,但它必须是嵌套类。

public class first {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    public class demo1
    {

        public class demo2
        {

        }
    }
}

#8


3  

Yes 200%,

是200%,

Example:

例:

class A {
 void methodDeclaration() { System.out.println("!!!"); }
 }
 class B {
 public static void main(String[] args) {
new A().methodDeclaration();
    }
 }

#9


2  

Yes it can,but there can only be 1 public class inside any package as java compiler creates the .Class file which is of the same name as the Public class name therefore if their are more than 1 public class it would be difficult to select for compiler that what should be the name of Class file.

是的它可以,但是在任何包中只能有1个公共类,因为java编译器创建的.Class文件与Public类名同名,因此如果它们是多于1个公共类,则很难选择编译器应该是什么类文件的名称。

#10


2  

I think it should be "there can only be one NON-STATIC top level public class per .java file". Isn't it?

我认为它应该是“每个.java文件只能有一个非STATIC顶级公共类”。不是吗?

#11


1  

Varies... One such example would be an anonymous classes (you'll encounter those alot when using event listeners and such).

变化......一个这样的例子就是一个匿名类(你在使用事件监听器时会遇到很多这样的事情)。

#12


1  

If you want to implement a singleton, that is a class that runs in your program with only one instance in memory throughout the execution of the application, then one of the ways to implement a singleton is to nest a private static class inside a public class. Then the inner private class only instantiates itself when its public method to access the private instance is called.

如果你想实现一个单例,这是一个在程序中运行的类,在整个应用程序的执行过程中只有一个实例在内存中,那么实现单例的一种方法是在公共类中嵌套一个私有静态类。然后内部私有类仅在调用其访问私有实例的公共方法时实例化自身。

Check out this wiki article,

查看这篇wiki文章,

https://en.wikipedia.org/wiki/Singleton_pattern

https://en.wikipedia.org/wiki/Singleton_pattern

The concept takes a while to chew on.

这个概念需要一段时间来咀嚼。

#13


1  

In a .java file,there can be only one public top-level class whose name is the same as the file, but there might be several public inner classes which can be exported to everyone and access the outer class's fields/methods,for example:AlertDialog.Builder(modified by 'public static') in AlertDialog(modified by 'public')

在.java文件中,只能有一个公共顶级类,其名称与文件相同,但可能有几个公共内部类可以导出到每个人并访问外部类的字段/方法,例如:AlertDialog.Builder(由'public static'修改)在AlertDialog中(由'public'修改)

#14


1  

Yes You can have more than one Class in one .Java file . But You have make one of them Public . and save .java file with same name as name of public class. when you will compile that .java file than you will get Separate .class files for each class defined in .java file .

是您可以在一个.Java文件中拥有多个类。但你已经将其中一个公之于众。并保存.java文件,其名称与公共类的名称相同。当您编译.java文件时,您将获得.java文件中定义的每个类的Separate .class文件。

Apart from this there are too many method for defining more than one class in one .java file .

除此之外,在一个.java文件中定义多个类的方法太多了。

  1. use concept of Inner Classes.
  2. 使用内部类的概念。
  3. Use Concept of Anonymous Classes .
  4. 使用匿名类的概念。

#15


1  

Yes ! .java file can contain only one public class.

是的! .java文件只能包含一个公共类。

If you want these two classes to be public they have to be put into two .java files: A.java and B.java.

如果您希望将这两个类公开,则必须将它们放入两个.java文件中:A.java和B.java。

#16


1  

Yes you can have more than one class inside a .java file. At most one of them can be public. The others are package-private. They CANNOT be private or protected. If one is public, the file must have the name of that class. Otherwise ANYTHING can be given to that file as its name.

是的,你可以在.java文件中有多个类。最多其中一个可以公开。其他包是私有的。它们不能是私密的或受保护的。如果一个是公共的,则该文件必须具有该类的名称。否则可以将该文件作为其名称赋予ANYTHING。

Having many classes inside one file means those classes are in the same package. So any other classes which are inside that package but not in that file can also use those classes. Moreover, when that package is imported, importing class can use them as well.

在一个文件中包含许多类意味着这些类在同一个包中。因此,该包内但不在该文件中的任何其他类也可以使用这些类。而且,当导入该包时,导入类也可以使用它们。

For a more detailed investigation, you can visit my blog post in here.

有关更详细的调查,您可以在此处访问我的博客文章。

#17


0  

There can only be one public class top level class in a file. The class name of that public class should be the name of the file. It can have many public inner classes.

文件中只能有一个公共类顶级类。该公共类的类名应该是该文件的名称。它可以有许多公共内部类。

You can have many classes in a single file. The limits for various levels of class visibility in a file are as follows:

您可以在一个文件中包含许多类。文件中各种级别可见性的限制如下:

Top level classes:
1 public class
0 private class
any number of default/protected classes

顶级类:1个公共类0个私有类任意数量的默认/受保护类

Inner classes:
any number of inner classes with any visibility (default, private, protected, public)

内部类:具有任何可见性的任意数量的内部类(默认,私有,受保护,公共)

Please correct me if I am wrong.

如果我错了,请纠正我。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/09/47537775a4798cc8d5a0de47c650f4cb.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com