-5

Is there any way this can be done in Java?

In PHP you can do $someVar = $this; and in JavaScript var something = this;

4

2 回答 2

8

Yes. Assuming your class name is MyClass

MyClass thisObject = this;

Alternatively, if you are in an inner, anonymous class, or something of the like, the following would be equivalent (given that you are in an instance of MyClass still)

MyClass thisObject = MyClass.this;
于 2012-07-12T04:54:07.047 回答
1

1. “this”与java中的非静态成员相关联。

2. this 代表当前对象

3. MyCustomClass mc = this ; 将当前对象分配给 mc,它是 MyCustomClass 类型的对象引用变量。

4.假设你在内部类,你可以做以下......

例如:

public class outer{

    int x = 10;

    class inner{

        int x=5;

        public void go(){
            System.out.println("Inner x: "+ this.x);           // Prints x in Inner class
            System.out.println("Inner x: "+ Outer.this.x);     // Prints x in Outer class
        }
    }
}
于 2012-07-12T06:22:24.637 回答