在Java中,您可以在单个文件中定义多个顶级类,只要其中一个是公共的(参见JLS§7.6).见下面的例子.
是否有此技术整洁名(类似于inner,nested,anonymous)?
JLS表示系统可能会强制执行这些辅助类不能的限制referred to by code in other compilation units of the package,例如,它们不能被视为包私有.这真的在Java实现之间发生了变化吗?
例如,PublicClass.java:
package com.example.multiple;
public class PublicClass {
PrivateImpl impl = new PrivateImpl();
}
class PrivateImpl {
int implementationData;
}
Run Code Online (Sandbox Code Playgroud) 在Java中,文件的名称应与该文件中public class包含的名称相同.为什么这是一个限制?它有什么用途?