class test { test me = this; private class subtest { subtest () { } } }
test me = this;就是说给test变量赋值为当前这个正在使用的对象实例, 比如下面的例子: public class TestTest{ public static void main(String[] args) { Test test = new Test(); test.setTest(); Test test2 = test.getTest(); System.out.println(test == test2); } } class Test { private Test test; public void setTest() { this.test = this; } public Test getTest() { return test; } } 打印结果是true,说明Test中的this就是指向的当前这个test对象,至于有什么好处,我也说不清楚,总之,this就是代表当前这个对象。同时,还有一个super,是代表当前对象的父类对象。
subtest里可以用me来引用上层this,不然要用test.this来引用
public class test { test me = this; public static void main(String[] args) { test t1 = new test(); System.out.println(t1.me == t1);//true --> this持有对自身对象的引用 } }
class test { test me=this; } 这里的this指的是test类创建的实体对象 可以这样理解test t=new test();this指的就是t实体
test me = this; private class subtest {
subtest () {
}
}
}
比如下面的例子:
public class TestTest{
public static void main(String[] args) {
Test test = new Test();
test.setTest();
Test test2 = test.getTest();
System.out.println(test == test2);
}
}
class Test {
private Test test; public void setTest() {
this.test = this;
} public Test getTest() {
return test;
}
}
打印结果是true,说明Test中的this就是指向的当前这个test对象,至于有什么好处,我也说不清楚,总之,this就是代表当前这个对象。同时,还有一个super,是代表当前对象的父类对象。
public class test {
test me = this;
public static void main(String[] args) {
test t1 = new test();
System.out.println(t1.me == t1);//true --> this持有对自身对象的引用
}
}
test me=this;
}
这里的this指的是test类创建的实体对象
可以这样理解test t=new test();this指的就是t实体