“this”的字段类型错误在JDK6的内部类中?
我在这里遇到了一个奇怪的结果,不确定它是 Java 中的错误还是预期的行为。我有一个内部类,我在其中使用反射来获取声明的字段(class.getDeclaredFields())。但是,当我循环字段列表并检查各个类型时,“this”字段返回外部类而不是内部类。
这是预期的行为吗?我觉得这很奇怪。
例如:
import java.lang.reflect.Field;
public class OuterClass {
public class InnerClass{
public String innerClassString;
public InnerClass innerClass;
}
public static void main(String[] args) {
// print the fields of the inner class
for( Field field : OuterClass.InnerClass.class.getDeclaredFields())
System.out.println( field.getName() + " ::: " + field.getType());
}
}
输出:
innerClassString ::: class java.lang.String
innerClass ::: class OuterClass$InnerClass
this$0 ::: class OuterClass
我期望 this$0 的类型为 OuterClass.InnerClass。
这是 Java 错误吗?有没有办法解决这种意外行为?
谢谢,
埃里克
I'm running into a strange result here and am not sure if it is a bug in Java or it is expected behaviour. I have an inner class on which I've used reflection to get the declared fields (class.getDeclaredFields()). However, when I loop over the list of fields and check the individual types, the "this" field returns the outerclass and not the inner class.
Is this expected behaviour? It seems quite odd to me.
Ex:
import java.lang.reflect.Field;
public class OuterClass {
public class InnerClass{
public String innerClassString;
public InnerClass innerClass;
}
public static void main(String[] args) {
// print the fields of the inner class
for( Field field : OuterClass.InnerClass.class.getDeclaredFields())
System.out.println( field.getName() + " ::: " + field.getType());
}
}
Output:
innerClassString ::: class java.lang.String
innerClass ::: class OuterClass$InnerClass
this$0 ::: class OuterClass
I expected this$0 to be of type OuterClass.InnerClass.
Is this a Java bug? Is there anyway to workaround this unexpected behaviour?
Thanks,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个非静态内部类都维护一个不可见的 ivar,它保存对其实例化的外部类的引用。这就是
this$0
。将 InnerClass 更改为
public static class
并查看差异。为了清楚起见,Oracle 建议使用以下术语:
http://download.oracle.com /javase/tutorial/java/javaOO/nested.html
===
在内部类的方法中,您可以说:
或者
this 的这个特殊变体 -
OuterClass.this
- 正在访问this$0
ivar - 它将返回OuterClass
实例。请注意,这与在InnerClass
方法中使用常规this
不同,后者将返回当前的InnerClass
实例。===
我不清楚你想做什么,所以我无法推荐如何实现你想要的。
Every non-static inner class maintains an invisible ivar that holds a reference to the outer class that it was instantiated for. That's what
this$0
is.Change InnerClass to
public static class
and see the difference.For clarity, Oracle recommends this terminology:
http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
===
In a method of the inner class, you can say:
or
This special variant of this -
OuterClass.this
- is accessing thethis$0
ivar - it will return theOuterClass
instance. Note that this is different than using the regularthis
inside anInnerClass
method, which will return the currentInnerClass
instance.===
I'm unclear what you are trying to do, so I can't recommend how to achieve what you want.
this$0 是内部类中的引用,它告诉外部类的哪个实例用于创建内部类的当前实例。
更详细的可以看这个问题。
在调试 Java 时,如果 IntelliJ IDEA 中的变量名称为“this$0”,这意味着什么?
this$0 is reference in Inner class which tells which instance of Outer class was used to create current instance of Inner class.
The more detail can see this question.
What does it mean if a variable has the name "this$0" in IntelliJ IDEA while debugging Java?