一个java文件可以有多个类吗? [英] Can a java file have more than one class?
问题描述
在一个 Java 文件中有多个类的目的是什么?我是 Java 新手.
What is the purpose of having more than one class in a Java file ? I am new to Java.
已这可以通过在公共类中创建内部类来实现,对吗?
Edited: That can be achieved by creating a inner class inside a public class, right?
推荐答案
是的,可以.但是,每个.java
文件只能有一个公共顶级类,并且公共顶级类必须与源文件同名.
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.
在一个源文件中包含多个类的目的是将相关的支持功能(内部数据结构、支持类等)与主要公共类捆绑在一起.请注意,不这样做总是可以的——唯一的影响是代码的可读性(或不可读).
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.
这篇关于一个java文件可以有多个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!