[英]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.