java “this.this$0”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27145010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the meaning of "this.this$0"?
提问by Hyman peterson
what's the meaning of "this.this$0" in this code? what does it stands for? I know why we use "this" but I have no idea about "this.this$0"
这段代码中“this.this$0”是什么意思?它代表什么?我知道为什么我们使用“this”,但我不知道“this.this$0”
class MainActivity 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.thispublic class SomeClass
{
int thispublic 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 thispublic 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();
}
}
; // saved copy of FixedStack.this
FixedStack$Enumerator(FixedStack this##代码##) {
this.this##代码## = this##代码##;
this.count = this##代码##.top;
}
int count;
public boolean hasMoreElements() {
return count > 0;
}
public Object nextElement() {
if (count == 0)
throw new NoSuchElementException("FixedStack");
return this##代码##.array[--count];
}
}
;
public SomeClass (int val)
{
this.this##代码## = val;
}
}
.ChangeToNumber(paramCharSequence.toString());
}
}
-----------------------or ----------------------
class MainActivity implements View.OnClickListener
{
public void onClick(View paramView)
{
this.this##代码##.startActivity(new Intent(this.this##代码##, about.class));
}
}
采纳答案by BinaryMan
this.this$0it's same to Main.access$0These mysterious symbols usually correspond to the anonymous inner classes. The Java VM doesn't know about them, only about top-level classes, so the Java compiler provides several workarounds to make inner classes to work.
this.this$0与Main.access$0相同, 这些神秘符号通常对应于匿名内部类。Java VM 不知道它们,只知道顶级类,因此 Java 编译器提供了几种解决方法来使内部类工作。
Local class has implicit reference to the instance of its enclosing class,'this$0' corresponds to this reference in the decompiled code. JVM prevents classes from accessing privates methods of other classes so the compiler generates several synthetic package-private methods like access$0 in order to access private methods of enclosing instance.
本地类对其封闭类的实例有隐式引用,'this$0' 对应于反编译代码中的此引用。JVM 阻止类访问其他类的私有方法,因此编译器生成几个合成的包私有方法,如 access$0,以便访问封闭实例的私有方法。
There are many others features of the Java language that are implemented with synthetic methods like generics and covariant return types.
Java 语言还有许多其他特性是通过合成方法实现的,例如泛型和协变返回类型。
I suggest you to check those links: Decoding Decompiled Source Code For Android
我建议您检查这些链接: Decoding Decompiled Source Code For Android
and : Performance Tips
和:性能提示
回答by Eran
There's nothing preventing you (beside decent naming conventions) from having an instance member called this$0
and then referring to it with the this
keyword.
没有什么可以阻止您(除了体面的命名约定)调用实例成员this$0
然后使用this
关键字引用它。
For example :
例如 :
##代码##回答by Kunj Bihari Vishwakarma
The Java 1.1 Language Specification specifies that the name of a type which is a class member, when transformed into Java 1.0 code for the purpose of generating Java virtual machine bytecodes, consists of the fully qualified name of the inner class, except that each .' character following a class name is replaced by a
$'. In addition, each inner class constructor receives the enclosing instance in a prepended argument. Here is how the transformed source code of the FixedStack example might look:
Java 1.1 语言规范指定作为类成员的类型的名称在转换为 Java 1.0 代码以生成 Java 虚拟机字节码时由内部类的完全限定名称组成,除了每个.' character following a class name is replaced by a
$' . 此外,每个内部类构造函数都在前置参数中接收封闭实例。以下是 FixedStack 示例的转换后的源代码的外观:
Anyone who has already programmed with Java or C++ adapter classes has written code similar to this, except that the link variables must be manually defined and explicitly initialized in top-level adapter classes, whereas the Java 1.1 compiler creates them automatically for inner classes.
任何已经使用 Java 或 C++ 适配器类进行编程的人都编写过与此类似的代码,不同之处在于必须在顶级适配器类中手动定义和显式初始化链接变量,而 Java 1.1 编译器会为内部类自动创建它们。
When the Enumerator needs to refer to the top or array fields of the enclosing instance, it indirects through a private link called this$0. The spelling of this name is a mandatory part of the transformation of inner classes to the Java 1.0 language, so that debuggers and similar tools can recognize such links easily. (Most programmers are happily unaware of such names.)
当 Enumerator 需要引用封闭实例的顶部或数组字段时,它会通过称为 this$0 的私有链接进行间接访问。该名称的拼写是将内部类转换为 Java 1.0 语言的强制性部分,以便调试器和类似工具可以轻松识别此类链接。(大多数程序员很高兴不知道这些名字。)
(Note: There is a limitation in some implementations of Java 1.1, under which the initialization of this$0 is delayed until after any superclass constructor is run. This means that up-level references made by a subclass method may fail if the method happens to be executed by the superclass constructor.)
(注意:在 Java 1.1 的一些实现中有一个限制,在这种限制下,this$0 的初始化被延迟到任何超类构造函数运行之后。这意味着子类方法进行的上级引用可能会失败,如果该方法碰巧由超类构造函数执行。)