[英]Inner class constructor invocation
The constructor of a non-private inner member class implicitly declares, as the first formal parameter , a variable representing the immediately enclosing instance of the class.
非私有内部成员类的构造函数隐式声明一个变量, 作为第一个形式参数 ,该变量表示该类的立即封闭实例。
Ok, if we write the following: 好吧,如果我们编写以下代码:
class A: A类:
package org.gradle;
public class A extends B.Inner{
public A(B b){
b.super(); //OK, invoke B.inner(B)
}
}
class B: B级:
package org.gradle;
public class B{
public class Inner{
}
}
As said here , b.super()
actually invoke B.Inner(B)
. 如这里所说,
b.super()
实际上调用B.Inner(B)
。
But if we write 但是如果我们写
class B: B级:
package org.gradle;
public class B {
class Inner{
public Inner(B b){
System.out.println("Inner(B)");
}
}
}
class A: A类:
package org.gradle;
public class A extends B.Inner{
public A(B b) {
b.super(); //The constructor B.Inner() is undefined
}
}
So, in the latter example b.super()
tries to invoke B.Inner()
instead. 因此,在后一个示例中,
b.super()
尝试改为调用B.Inner()
。 Why is that so difference? 为什么如此不同?
It does try to invoke B.Inner(B)
in your second example. 在第二个示例中,它确实尝试调用
B.Inner(B)
。
It can't find it because there's only a B.Inner(B, B)
. 因为只有
B.Inner(B, B)
而找不到它。 If your constructor is B.Inner()
then it gets changed to Inner(B)
... and if your constructor is B.Inner(B)
then it gets changed to B.Inner(B, B)
. 如果您的构造函数是
B.Inner()
则将其更改为Inner(B)
...,如果您的构造函数是B.Inner(B)
则将其更改为B.Inner(B, B)
。
Note that the hidden parameter is effectively an implementation detail, and unless you're studying how the Java compiler works, you don't need to know it exists. 请注意,hidden参数实际上是实现细节,除非您正在研究Java编译器的工作方式,否则不需要知道它的存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.