“this.this $ 0”是什么意思?

时间:2014-11-26 09:04:54

标签: java android

" this.this $ 0"的含义是什么?在这段代码? 它代表什么? 我知道为什么我们使用"这个"但我不知道" this.this $ 0"

 class MainActivity$1 implements TextWatcher
{
  public void afterTextChanged(Editable paramEditable)
  {
  }

  public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
  {
  }

  public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
  {
    this.this$0.ChangeToNumber(paramCharSequence.toString());
  }
}
-----------------------or ----------------------
class MainActivity$2 implements View.OnClickListener
{
  public void onClick(View paramView)
  {
    this.this$0.startActivity(new Intent(this.this$0, about.class));
  }
}

4 个答案:

答案 0 :(得分:15)

this.this $ 0 Main.access $ 0相同 这些神秘的符号通常对应于匿名的内部类。 Java VM并不了解它们,只关于顶级类,因此Java编译器提供了几种解决方法来使内部类工作。

本地类隐含引用其封闭类的实例,'这个$ 0'对应于反编译代码中的此引用。 JVM阻止类访问其他类的privates方法,因此编译器会生成几个合成的包私有方法,如访问$ 0,以便访问封装实例的私有方法。

Java语言的许多其他功能都是使用泛型和协变返回类型等合成方法实现的。

我建议你查看这些链接: Decoding Decompiled Source Code For Android

和:Performance Tips

答案 1 :(得分:3)

没有什么能阻止你(除了不错的命名约定)有一个名为this$0的实例成员,然后使用this关键字引用它。

例如:

public class SomeClass
{
    int this$0;
    public SomeClass (int val) 
    {
        this.this$0 = val;
    }
} 

答案 2 :(得分:2)

Java 1.1语言规范指定作为类成员的类型的名称,当为了生成Java虚拟机字节码而转换为Java 1.0代码时,由内部类的完全限定名称组成,除了每个.' character following a class name is replaced by a $'。此外,每个内部类构造函数都在前置参数中接收封闭实例。以下是FixedStack示例的转换源代码的外观:

public class FixedStack {
        ... (the methods omitted here are unchanged)
        public java.util.Enumeration elements() {
            return new FixedStack$Enumerator(this);
        }
    }

    class FixedStack$Enumerator implements java.util.Enumeration {
        private FixedStack this$0; // saved copy of FixedStack.this
        FixedStack$Enumerator(FixedStack this$0) {
            this.this$0 = this$0;
            this.count = this$0.top;
        }

        int count;
        public boolean hasMoreElements() {
            return count > 0;
        }
        public Object nextElement() {
            if (count == 0)
                throw new NoSuchElementException("FixedStack");
            return this$0.array[--count];
        }
    }

任何已使用Java或C ++适配器类编程的人都编写了与此类似的代码,除了必须手动定义链接变量并在顶级适配器类中显式初始化,而Java 1.1编译器会自动为内部创建它们类。

当Enumerator需要引用封闭实例的顶部或数组字段时,它会通过名为$ 0的私有链接进行间接传输。此名称的拼写是内部类转换为Java 1.0语言的必需部分,因此调试器和类似工具可以轻松识别此类链接。 (大多数程序员都很高兴不知道这些名字。)

(注意:在Java 1.1的某些实现中存在一个限制,在该实现中,$ 0的初始化被延迟,直到运行任何超类构造函数之后。这意味着子类方法所做的上级引用可能会失败,如果方法碰巧由超类构造函数执行。)

答案 3 :(得分:0)

这个$ 0通常用于non-static内部类的父对象。如,

public class Outer {
    class Inner1 {
        int f1 = 1;
    }

    static class Inner2 {
        int f1 = 2;
    }    

    public static void main(String[] args) {
        Outer o = new Outer();
        Outer.Inner1 i1 = o.new Inner1(); //weird but valid
        Outer.Inner2 i2 = new Outer.Inner2(); //normal
        //wrong: Outer.Inner1 i3 = new Outer.Inner1();
    }
}

通常我们将内部类定义为statici2只有一个字段,但i1有一个额外的this$0,指向o

enter image description here