简体   繁体   English

内部类的默认构造函数是否需要外部类的对象?

[英]Does default constructor for inner class need an object of an outer class?

I was fiddling around with java, and I created two classes, Outer and Inner 我在摆弄Java,并创建了两个类, Outer类和Inner

public class Outer {
    public class Inner { 
    }
}

Inner class is using default constructor. 内部类正在使用默认构造函数。 I can create new instance of Inner class inside Outer just by calling non-args default constructor new Inner() . 我可以通过调用非参数默认构造函数new Inner()Outer创建Inner类的新实例。 But when I tried do same thing using reflection, I noticed constructor require Outer type object. 但是当我尝试使用反射来做同样的事情时,我注意到构造函数需要Outer类型对象。

Is that mean inner class default constructor is not non-args? 这是否意味着内部类默认构造函数不是non-args? Why there is disjoint between calling constructor in normal way and reflection? 为什么正常调用构造函数和反射之间不相交?

There is no "disjoint between calling constructor in normal way and reflection", only between calling constructor from inside the Outer class and from outside of Outer class. 在“以正常方式调用构造函数和反射之间没有脱节”,只有在从Outer类内部和从Outer类外部调用构造函数之间没有。

Since the Inner class is not static , it has a reference to Outer . 由于Inner类不是static ,因此它具有对Outer的引用。 If you want to create an instance of Inner from outside of Outer , you must write it like this: 如果要从Outer外部创建Inner实例,则必须这样编写:

Outer outerObj = new Outer();
Outer.Inner innerObj = outerObj.new Inner();

The requirement to provide outerObj to the constructor of Inner is exactly the same as when you instantiate the Inner class through reflection. 提供要求outerObj到的构造函数Inner是完全一样的,当你实例化的Inner通过反射类。 The only difference is that the syntax places outerObj on the left of operator new , while reflection passes outerObj to the constructor. 唯一的区别是语法在操作符new的左侧放置了outerObj ,而反射将outerObj传递给了构造函数。 Java compiler adds a hidden argument to the constructor that it generates automatically, in the same way that it adds a hidden field to the generated class in order to hold a reference to the Outer object. Java编译器向其自动生成的构造函数中添加了一个隐藏参数,其方式与向已生成的类中添加隐藏字段以保持对Outer对象的引用相同。

Note that this is true only for non-static inner classes. 请注意,这仅适用于非静态内部类。 When your inner class is static , there is no hidden parameter and no hidden field, so reflection lets you create instances of the inner class without an Outer object. 当内部类为static ,没有隐藏的参数,也没有隐藏的字段,因此反射使您可以创建内部类的实例而无需使用Outer对象。

内部类不是静态的,因此只有在拥有外部对象时才能创建内部实例。

I can create new instance of Inner class inside Outer just by calling non-args default constructor new Inner() 我可以在外部内部创建新的内部类实例,只需调用非参数默认构造函数new Inner()

Inside yes, but his won't compile outside of Outer or from a static method of Outer : 里面是的,但他不会编译外或从一个静态方法外Outer

new Outer.Inner();

This is because when you call new Inner() inside Outer this is implicitly passed to the constructor such that this: 这是因为当您在Outer内部调用new Inner()this隐式传递给了构造函数,使得:

public class Outer {
    public class Inner {
    }

    public example(){
        new Inner();
    }
}

Is equivalent to this static version: 等效于此静态版本:

public class Outer {
    public static class Inner {
        private Outer mOuter;
        public Inner(Outer outer){
            mOuter = outer;
        }
    }

    public example(){
        new Inner(this);
    }
}

The hidden reference to the Outer allows the inner to invoke methods on Outer as though they are it's own. Outer的隐藏引用允许内部调用Outer方法,就好像它们是自己的一样。

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

相关问题 内部类对象作为外部类构造函数参数 - Inner class object as Outer class constructor argument 我们可以在外部类的构造函数中创建内部类的对象吗? - Can we create an object of the inner class in the constructor of the outer class? Java - 私有内部类的对象,作为外部类构造函数的参数 - Java - object of a private inner class being as an argument to an outer class' constructor 更改内部类对象的外部类引用 - Changing outer class reference for an object of inner class 为什么在内部 class 构造函数中没有外部 class object 的 LocalVariableTable 条目(Java 字节码) - Why isn't there an entry in LocalVariableTable for outer class object in an inner class constructor(Java Bytecode) 如何在外部类构造函数中创建内部类的实例 - How do I create an instance of a inner class in the outer class constructor 在外部类的构造函数中实例化内部类是否危险? (JAVA) - Is it dangerous to instantiate an inner class within the outer class's constructor? (Java) 如何通过外部类构造函数访问内部类? - How to access a inner class through an outer class constructor? Java - 内部类构造函数 - 仅允许外部类 - Java - Inner class constructor - allowed for outer class only 从外部类非静态构造函数访问内部类的静态方法 - Accessing static methods of inner class from an outer class nonstatic constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM