单个文件中的多个类:此处不允许修饰符 private
我无法理解为什么这段代码无法编译:
class A {
public static void main(String[] args) {
System.out.println("hi");
}
}
private class B {
int a;
}
我将内容保存在名为 A.java
的文件中 - 并且出现错误:
modifier private not allowed here // where I have defined class B
当我尝试 B 作为私有和受保护。有人可以向我解释一下这背后的原因吗?
谢谢 !
I am not able to understand why this code doesn't compile:
class A {
public static void main(String[] args) {
System.out.println("hi");
}
}
private class B {
int a;
}
I am saving the contents in a file named A.java
- and I get an error:
modifier private not allowed here // where I have defined class B
This happens both when I try B as private and protected. Can someone please explain me the reason behind this?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使 B 嵌套在 A 中,如下所示:
或将 B 移动到单独的文件中。您也可以坚持使用默认访问级别,
这样只能从包内访问该类:
Make the B nested of A, like this:
Or move B to a separate file. Also you can stick with default access level,
this way the class can be accessed only from within the package:
private 和 protected 对于顶级(非成员)类/接口来说是没有意义的。
它们仅适用于类成员,可以是变量、常量、构造函数、方法、类和接口。
原因:
(1) 私有:如果我们将一个类定义为私有,其意义/目的可能是什么。它的范围应该是某个区域私有的。默认访问权限已经是包私有的。没有人希望一个类是源文件私有的,(猜测原因)允许它可能不是一个好的编程实践,因为java应用程序最终以包的形式组织,而不是以源文件的形式组织。任何源文件都应该是某个包的一部分,因此在广泛/最终视图中,每个类/接口都是某个包的一部分,而不仅仅是某个 .java 文件的一部分。所以不适用。
(2) 受保护:如果某个东西受到保护,那么它应该只在包内可用,并且只能对其他包中的子类可用。要扩展不同包中的类,它应该可用于其他包中的所有类,但 protected 表示类应该仅可用于扩展它的类。这是一种僵局。所以不适用。
来源:我的阅读和理解
private and protected are meaningless to be allowed to a top level(not member) class/interface.
They are applicable only to the class members which can be variables, constants, constructors, methods, classes, and interfaces.
Why:
(1) private: What may be the meaning/purpose if we define a class as private. Its scope should be private to some area. default access is already package private. And nobody wants a class to be source file private, (Guessing the reason) it may not be a good programming practice to allow because java applications are finally organized in the form of packages, but not in terms of source files. Any source file should be part of some package, so in broad/final view each class/interface is part of some package, not just of some .java file. So not applicable.
(2) protected: If something is protected it should be available only within package and only to the sub classes in other packages. To extend a class in a different package, it should be available to all the classes in other packages, but protected says class should be available only to the classes extended it. It's a kind of deadlock situation. So not applicable.
Source: My readings and understanding
只是根本没有 private/protected 修饰符。
Just have no private/protected modifier at all.
B 需要对某些东西是私有的。将其放在类 A 的定义中或创建另一个文件 B.java,并在那里定义它,但它不能是私有的。
B needs to be private to something. Place it within the definition of class A or create another file, B.java, and define it there, but then it cannot be private.
如果您不对该类使用 public 关键字,则默认情况下它将是私有的(仅在文件中可见)。
每个 .java 文件只能有一个公共类,所有其他类都必须是私有的。因此,在您的示例中,A 类可以是公共的,而 B 类不需要任何修饰符。公共类名必须与.java 文件名匹配(例如,A.java 只能包含一个名为“A”的公共类)。
If you don't use the public keyword for the class it will be private by default (visible only within the file).
There can be only one public class per .java file, all the others need to be private. So class A can be public and class B doesn't need any modifiers in your example. The public class name must match the .java file name (eg. A.java can only contain one public class called "A").
A.java
不能包含两个类。A.java
cannot contain two classes.来自 Java 语言规范:
因此,是的,顶级类声明不允许使用 private 和 protected 修饰符。
顶级类可以是公共的,也可以不是,但不允许使用
私有
和受保护的
。如果该类被声明为公共,则可以从任何包中引用它。否则只能从同一个包(命名空间)中引用它。私有顶级类没有多大意义,因为任何类都无法引用它。根据定义,它将无法使用。
private
对于成员类来说可以使一个类仅可引用它的封闭类。受保护的成员类可以从 (1) 同一包的任何类和 (2) 封闭类的任何子类引用。将这个概念映射到顶级类是很困难的。第一种情况由没有访问修饰符的顶级类覆盖。第二种情况不适用于顶级类,因为没有封闭类或来自与该类有特殊关系的不同包的其他内容(如子类)。因此,我认为
protected
是不允许的,因为它的底层概念不适用于顶级类。From the Java Language specification:
So yes, the private and the protected modifiers are not allowed for top level class declarations.
Top-level classes may be public or not, while
private
andprotected
are not allowed. If the class is declared public, then it can be referred to from any package. Otherwise it can only be referred to from the same package (namespace).A private top level class wouldn't make much sense because it couldn't be referred to from any class. It would be unusable by definition.
private
is OK for member classes to make a class referable to only it's enclosing class.A protected member class can be referred to from (1) any class of the same package and from (2) any subclass of the enclosing class. Mapping this concept to top level classes is difficult. The first case is covered by top level class with no access modifiers. The second case is not applicable for top level classes, because there is no enclosing class or something else from a different package with a special relation to this class (like a subclass). Because of this I think,
protected
is not allowed because it's underlying concept is not applicable for top level classes.