java中,使用this的地方,完全可以不使用this,this代表当前对象,对于当前对象的方法或者属性,个人觉得用不用this都没有多大关系,示例代码如下:
package testTHIS;
public class TestTHIS {
int flag = 0;
public static void main(String[] args) {
Test test = new Test();
test.main();
TestTHIS tt = new TestTHIS();
tt.say(); // 不能使用this.say();
}
public void say() {
MyTest mt = new MyTest();
mt.main();
int i = this.flag;
int k = flag;
}
}
class Test {
public void main() {
say1();
this.say1();
say2();
this.say2();
say3();
this.say3();
say4();
this.say4();
}
public void say1() {
System.out.println("111111111111111");
}
protected void say2() {
System.out.println("222222222222222");
}
void say3() {
System.out.println("333333333333333");
}
private void say4() {
System.out.println("444444444444444");
}
}
class MyTest extends Test {
@Override
public void main() {
this.say1();
}
}
内部类需要引用外部类的实例对象
局部变量和全局变量名称重复,需要用
this
指定引用的是全局变量建造者模式的
return this
应该也算吧构造函数调用其它重载构造函数