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

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

Can a java file have more than one class?

javaclass

提问by yesraaj

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?

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

采纳答案by Sean Reilly

Yes, it can. However, there can only be one public top-levelclass per .javafile, 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.

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

回答by laginimaineb

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

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

回答by Meetu Choudhary

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 文件,该文件与公共类名称同名,因此如果它们的公共类超过 1 个,则很难选择编译器认为类文件的名称应该是什么。

回答by kgiannakakis

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 类通常需要一个单独的文件才能公开。这样它就与使用它的类捆绑在一起。

回答by Confusion

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 的“任务”类。它们可能很小,最好与创建和调用它们的父类结合使用。

回答by Rich Apodaca

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.

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

回答by Alberto

Yes, as many as you want!

是的,你想要多少就多少!

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

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

回答by HolidayCactus

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

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

回答by Utsav

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.

一个编译单元中只能有一个公共类/接口。cu 的名称必须与此公共顶级类型完全相同。

//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 部分涵盖的课程数量及其访问级别的要点)

回答by chrismjh

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,

看看这篇维基文章,

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

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

The concept takes a while to chew on.

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