java - 为什么子类可以调用父类的私有构造方法
高洛峰
高洛峰 2017-04-17 17:50:33
0
3
433

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
洪涛
public class UnSafeSequence {
    public class TestMath{
        private TestMath(){
            System.out.println("父类实例化");
        }
    }
    
    public class TestMath1 extends TestMath{
        public TestMath1(){
            System.out.println("子类实例化");
        }
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(new UnSafeSequence().new TestMath1());
        
    }
}

Description of private modifiers, definitions of top-level classes and inner classes in the Java 6 language specification

6.6.1
if the member or constructor is declared private, then 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.
If a class If a member or constructor is declared private, then only the top-level class that declares the member or constructor has access (of course the class that declares the member and constructor is also accessible)

8.
A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface. .
The top-level class is not a nested class (inner class). A nested class (inner class) is a class declared in other classes or interfaces

In view of the above description, the external class can access the TestMath internal class whose constructor is marked as private.
TestMath1 is also an internal class, which inherits another internal class TestMath. Because an internal class depends on the external class instance object to exist, it will be implicitly associated with an external class instance
So

    public class TestMath1 extends TestMath{
        public TestMath1(){
            System.out.println("子类实例化");
        }
    }

can be written as

    public class TestMath1 extends UnSafeSequence.TestMath{
        public TestMath1(){
            UnSafeSequence.this.super();
            System.out.println("子类实例化");
        }
    }

This can explain why a subclass of an inner class can access the private constructor of its parent class

刘奇

Generally speaking, subclasses cannot call the private constructor of the parent class.
The two classes you have here are both member inner classes of the same class. The inner class can freely access the member variables of the outer class, even if they are private. Therefore, a member inner class can access another member inner class (because it can be regarded as a member variable), and the accessed member inner class is completely unprotected from the member inner class accessing it.

大家讲道理

Inner classes can inherently access the private methods and fields of any external class. The TestMath inherited by TestMath1 is itself the internal class of UnSafeSequence, so TestMath1 can access any private methods and fields defined in UnSafeSequence, including the private methods and fields in TestMath.

If you define TestMath separately outside UnSafeSequence, then TestMath1 cannot access the private methods and fields in TestMath.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template