简体   繁体   English

内部类可以访问基类中的私有final方法,但是为什么呢?

[英]inner class have access to private final methods in base class but why?

Why creators of java allowed this situation? 为什么Java的创建者允许这种情况? I am sure there must be some reason for it. 我确信一定有某些原因。 My below code allows Lion to mischievously run as fast as Cheetah . 我的以下代码允许Lion恶作剧地运行起来与Cheetah一样快。

public class animal {
    class carnivores {
       private final void runAsFastAsCheetah() {
           System.out.println("Ran as fast as Cheetah");
        }
    }
    public class Lion extends carnivores {
       public void runAsFastAsLion() {
           System.out.println("Ran as fast as Lion.");
           super.runAsFastAsCheetah();
        }
    }
    public static void main(String[] args) {
        animal animal = new animal();
        Lion lion = animal.new Lion();
        //lion.runAsFastAsCheetah(); //Not allowed but//
        lion.runAsFastAsLion();
    }
}

EDIT: For those taking Lion and cheetah seriously, I have modified code. 编辑:对于那些认真对待狮子和猎豹的人,我已经修改了代码。

public class foo {
    class A {
        private final void myMethod() {
            System.out.println("in private final myMethod()");
        }
    }
    public class B extends A {
        public void myMethod() {
            System.out.println("in B's myMethod()");
            super.myMethod();
        }
    }
    public static void main(String[] args) {
        foo foo = new foo();
        B b = foo.new B();
        b.myMethod();
    }
}

All classes with the same outer class can access private members of any other class of the same outer. 具有相同外部类的所有类都可以访问相同外部的任何其他类的private成员。 This features was added when nested classes were added. 添加嵌套类时添加了此功能。 IMHO this was because these members are compiled together and it makes nested classes more useful. 恕我直言,这是因为这些成员被一起编译,并使嵌套类更有用。

Note: The JVM doesn't support this feature, and thus the compiler add accessor methods which appear the the stack traces like access$100 . 注意:JVM不支持此功能,因此编译器添加了访问器方法,这些方法出现在堆栈跟踪中,如access$100 These are added by the compiler to allow access to private members between classes. 这些由编译器添加,以允许访问类之间的私有成员。


Access modifiers only check one level. 访问修饰符仅检查一个级别。 If A can access B and B and access C, then A can access anything B lets it access which could be C. 如果A可以访问B和B并访问C,则A可以访问B所允许访问的任何内容(可能是C)。

The reason this is don't is to avoid making private meaningless. 这样做不是为了避免使私有变得毫无意义。 If a private member could only be accessed by class which could access it, it would mean it could only be called by a main in the same class. 如果private成员只能由可以访问它的类访问,这意味着它只能由同一类中的主调用。 This would make it useless in any other class. 这将使其在任何其他类中都没有用。

From the Java Language Specification : 根据Java语言规范

A member (class, interface, field, or method) of a reference type, or a constructor of a class type, is accessible only if the type is accessible and the member or constructor is declared to permit access: 引用类型的成员(类,接口,字段或方法)或类类型的构造函数仅在可访问类型且声明该成员或构造函数允许访问的情况下才可访问:

  • ... ...
  • Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. 否则,成员或构造函数将被声明为私有,并且仅当且仅当它出现在封装成员或构造函数的声明的顶级类(第7.6节)的主体内时,才允许访问。

In other words, within a top-level class, private and protected no longer apply. 换句话说,在顶级类中, privateprotected不再适用。

As for the why, well if you think of everything in a class being accessible by that class, then that includes everything inside any inner classes too! 至于为什么,那么,如果您认为该类可以访问该类中的所有内容,那么它也包括所有内部类中的所有内容!

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

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