spring - java中关键字this的用法
本文介绍了spring - java中关键字this的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
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
应该也算吧构造函数调用其它重载构造函数
这篇关于spring - java中关键字this的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文