简体   繁体   English

Java-私有内部类公共成员

[英]Java - private inner class public member

how does the access level modifier change the behavior of a member inside an inner private class? 访问级别修饰符如何改变内部私有类中成员的行为? I have this code 我有这个代码

class Main {
    private static class Inner {
        private static int a = 1;
    }
    public static void main(String[] args) {
        System.out.print(Inner.a);
    }
}

I can access the attribute "a" from the outer class either it's access level is public or private (and this is already weird for me). 我可以从外部类访问属性“ a”,无论它的访问级别是公共的还是私有的(这对我来说已经很奇怪了)。 The question is: if the attribute were " public static int ", what were the difference (considering that the Inner class is private so inaccessible from the extern of the "Main" class)? 问题是:如果该属性为“ public static int ”,则有什么区别 (考虑到Inner类是私有的,因此从“ Main”类的外部无法访问)?

To recap 回顾

  • The attribute is private: I can access it from Main but not from outside Main (because Inner is private) 该属性是私有的:我可以从Main访问它,但是不能从Main外部访问它(因为Inner是私有的)
  • The attribute is public: I can still access it from Main but not from outside Main (because Inner is private) 该属性是public:我仍然可以从Main来访问它,但是不能从Main外部访问它(因为Inner是私有的)

I can see no difference 我看不出有什么不同

Inner is private, it can be accessed only from within Main . Inner是私有的,只能从Main内部访问。 Even if a is public, but Inner is private and cannot be accessed from a particular place, attribute of a would be irrelevant. 即使a是公开的,但Inner是私有的,不能从某一特定地点访问的属性a是无关紧要的。

class Main {
    private static class Inner {
        public static int a = 1;
    }
    public static void main(String[] args) {
        System.out.print(Inner.a);
    }
}

When you run , 跑步时

1

Whenever Inner is static in the same class,( Main ), Inner (and all its members) are always accessible from anywhere in the Main class. 只要Inner在同一个类中是静态的( Main ),就始终可以从Main类中的任何位置访问Inner (及其所有成员)。 It is private outside of Main Class. 它是Main Class以外的私有对象。 If I were to access 如果我要访问

Main main = new Main();
main.Inner; // Compilation Failure. Inner is not accessible
// Technically I would never do new Main(), but to illustrate I am doing so.

Or a more sensible example of the same 还是一个更明智的例子

class Main {
    public static void main(String[] args) {
        System.out.print(SomeClass.Inner);
    }
}

class SomeClass{
    private static class Inner {
        public static int a = 1;
    }

}

To illustrate the difference of making a private or public, consider the following two examples 为了说明做的差a私人或公共,考虑下面两个例子

class Main {
    public static void main(String[] args) {
        System.out.print(SomeClass.Inner.a);
    }
}

class SomeClass{
    public static class Inner {
        private static int a = 1;  // If a is private, it produces an error above
      //public static int a = 1; // This would not be an error
    }
}

So in your particular case, as long as it is in same class Main , it is irrelevant, but if it is outside as next example, it would matter. 因此,在您的特定情况下,只要它在Main类中,就没有关系,但是如果在下一个示例中它在外部,则将很重要。

You can think of your Inner class as a class-level attribute. 您可以将您的Inner类视为类级别的属性。 Making it private prevents access to it outside of the class. 将其设为私有可防止在类之外访问它。 Since it's an inner class, the inner class's variables belong to the outer class and are thus accessible to the outer class, regardless of the access modifiers. 由于它是内部类,因此内部类的变量属于外部类,因此,无论访问修饰符如何,外部类都可以访问它们。

http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM