单个文件中的多个类:此处不允许修饰符 private

发布于 2024-09-14 03:58:17 字数 380 浏览 7 评论 0原文

我无法理解为什么这段代码无法编译:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风追烟花雨 2024-09-21 03:58:18

使 B 嵌套在 A 中,如下所示:

class A {
    public static void main(String[] args) {
        System.out.println("hi");
    }

    private class B {
        int a;
    }
}

或将 B 移动到单独的文件中。您也可以坚持使用默认访问级别,
这样只能从包内访问该类:

class A {
    public static void main(String[] args) {
        System.out.println("hi");
    } 
}

class B {
    int a;
}

Make the B nested of A, like this:

class A {
    public static void main(String[] args) {
        System.out.println("hi");
    }

    private class B {
        int a;
    }
}

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:

class A {
    public static void main(String[] args) {
        System.out.println("hi");
    } 
}

class B {
    int a;
}
£噩梦荏苒 2024-09-21 03:58:18

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

鹿港小镇 2024-09-21 03:58:18

只是根本没有 private/protected 修饰符。

Just have no private/protected modifier at all.

梦晓ヶ微光ヅ倾城 2024-09-21 03:58:18

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.

执笔绘流年 2024-09-21 03:58:18

如果您不对该类使用 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").

无畏 2024-09-21 03:58:18

A.java 不能包含两个类。

A.java cannot contain two classes.

逐鹿 2024-09-21 03:58:17

来自 Java 语言规范:

访问修饰符 protected 和 private 仅适用于直接封闭类声明中的成员类

因此,是的,顶级类声明不允许使用 private 和 protected 修饰符。

顶级类可以是公共的,也可以不是,但不允许使用私有受保护的。如果该类被声明为公共,则可以从任何包中引用它。否则只能从同一个包(命名空间)中引用它。

私有顶级类没有多大意义,因为任何类都无法引用它。根据定义,它将无法使用。 private 对于成员类来说可以使一个类仅可引用它的封闭类。

受保护的成员类可以从 (1) 同一包的任何类和 (2) 封闭类的任何子类引用。将这个概念映射到顶级类是很困难的。第一种情况由没有访问修饰符的顶级类覆盖。第二种情况不适用于顶级类,因为没有封闭类或来自与该类有特殊关系的不同包的其他内容(如子类)。因此,我认为 protected 是不允许的,因为它的底层概念不适用于顶级类。

From the Java Language specification:

The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration

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 and protected 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文