将最终变量传递给匿名类
在通过构造函数传递给匿名类的最终变量中, Jon Skeet 提到变量通过自动生成的构造函数传递给匿名类实例。为什么在这种情况下我看不到使用反射的构造函数:
public static void main(String... args) throws InterruptedException {
final int x = 100;
new Thread() {
public void run() {
System.out.println(x);
for (Constructor<?> cons : this.getClass()
.getDeclaredConstructors()) {
StringBuilder str = new StringBuilder();
str.append("constructor : ").append(cons.getName())
.append("(");
for (Class<?> param : cons.getParameterTypes()) {
str.append(param.getSimpleName()).append(", ");
}
if (str.charAt(str.length() - 1) == ' ') {
str.replace(str.length() - 2, str.length(), ")");
} else
str.append(')');
System.out.println(str);
}
}
}.start();
Thread.sleep(2000);
}
输出是:
100
constructor : A$1()
In final variable passed to anonymous class via constructor, Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor. Why would I not be able to see the constructor using reflection in that case:
public static void main(String... args) throws InterruptedException {
final int x = 100;
new Thread() {
public void run() {
System.out.println(x);
for (Constructor<?> cons : this.getClass()
.getDeclaredConstructors()) {
StringBuilder str = new StringBuilder();
str.append("constructor : ").append(cons.getName())
.append("(");
for (Class<?> param : cons.getParameterTypes()) {
str.append(param.getSimpleName()).append(", ");
}
if (str.charAt(str.length() - 1) == ' ') {
str.replace(str.length() - 2, str.length(), ")");
} else
str.append(')');
System.out.println(str);
}
}
}.start();
Thread.sleep(2000);
}
The output is:
100
constructor : A$1()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在本例中,这是因为 100 是一个常数。这会融入你的课堂。
如果将
x
更改为:... 那么您将在输出中看到
Test$1(int)
。 (尽管没有明确声明,但捕获更多变量会向构造函数添加参数。)In this case, it's because 100 is a constant. That gets baked into your class.
If you change
x
to be:... then you'll see
Test$1(int)
in the output. (This is despite it not being explicitly declared. And yes, capturing more variables adds parameters to the constructor.)以下是您的程序在我的系统上打印出来的内容:
所以构造函数就在那里。然而,它是无参数的。从反汇编来看,编译器发现它不需要将 x 传递给 run() ,因为它的值在编译时已知。
如果我像这样更改代码:
现在生成的构造函数是:
唯一的参数是
x
的值。Here is what your program prints out on my system:
So the constructor is there. However, it is parameterless. From looking at the disassembly, what happens is that the compiler figures out that it doesn't need to pass
x
torun()
since its value is known at compile time.If I change the code like so:
The constructor that gets generated is now:
The sole argument is the value of
x
.