什么时候需要将java类X放入名为X.java的文件中?
伙计们,我遇到过这样的法律行为:
文件B.java:
final class C {} final class D {}
文件A.java:
class B {} public class A {}
问题:
- 什么时候需要将类X放入自己的X.java文件中?课堂可见性/最终结果在这里重要吗李>
- 关于这个类/java关系有官方规范吗李>
非常感谢
你可以在下面搜索框中键入要查询的问题!
伙计们,我遇到过这样的法律行为:
文件B.java:
final class C {} final class D {}
文件A.java:
class B {} public class A {}
问题:
非常感谢
# 1 楼答案
在大多数实现中,事实上的标准是源文件只能包含一个顶级
public
类型定义。源文件的名称必须是该类型的名称源文件可以包含嵌套类型,但根据定义,它们不是顶级
public
类型这在Java语言规范中是推荐的,但不是必需的
请注意
final
与可访问性无关,因此这与此无关相关问题
另见
# 2 楼答案
Sun/Oracle编译器允许每个文件最多有一个公共顶级类,在这种情况下,文件名必须是类名
{a1}不强制要求这种行为,但明确允许:
# 3 楼答案
公共类类名必须位于名为ClassName的文件中。爪哇
非公立课程没有这样的限制
这样做的结果是,一个Java源文件只能有一个公共类,但可以有任意多个非公共类