“this”的字段类型错误在JDK6的内部类中?

发布于 2024-12-02 13:10:03 字数 911 浏览 0 评论 0原文

我在这里遇到了一个奇怪的结果,不确定它是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-12-09 13:10:03

每个非静态内部类都维护一个不可见的 ivar,它保存对其实例化的外部类的引用。这就是 this$0

将 InnerClass 更改为 public static class 并查看差异。

为了清楚起见,Oracle 建议使用以下术语:

http://download.oracle.com /javase/tutorial/java/javaOO/nested.html

术语:嵌套类分为两类:静态类和非静态类。 声明为静态的嵌套类简称为静态嵌套类非静态嵌套类称为内部类

...

InnerClass 的实例只能存在于 OuterClass 的实例中,并且可以直接访问其封闭实例的方法和字段。 ...

要实例化内部类,必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

===

如果需要的话,是否可以从内部类内部访问 this$0 变量?

在内部类的方法中,您可以说:

OuterClass outer = OuterClass.this;

或者

System.out.println(OuterClass.this.toString());

this 的这个特殊变体 - OuterClass.this - 正在访问 this$0 ivar - 它将返回OuterClass实例。请注意,这与在 InnerClass 方法中使用常规 this 不同,后者将返回当前的 InnerClass 实例。

===

此外,如何确定我是否正在处理“this$0”字段而不使用字符串比较?

我不清楚你想做什么,所以我无法推荐如何实现你想要的。

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

Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

...

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. ...

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

===

Is there any access to the this$0 variable then from within the inner class if you want?

In a method of the inner class, you can say:

OuterClass outer = OuterClass.this;

or

System.out.println(OuterClass.this.toString());

This special variant of this - OuterClass.this - is accessing the this$0 ivar - it will return the OuterClass instance. Note that this is different than using the regular this inside an InnerClass method, which will return the current InnerClass instance.

===

Furthermore, how can I determine if I am processing the "this$0" field without using a string compare?

I'm unclear what you are trying to do, so I can't recommend how to achieve what you want.

带刺的爱情 2024-12-09 13:10:03

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文