this 表示当前类的一个对象
-
局部变量和全局变量同名,可是使用this.变量名,来访问全局变量
-
this可以用来调方法
本类方法 。不用实例化自己,主类中不可以,有static修饰也不可以用this关键字,方法中可以调用方法 但是不可以写方法定义方法
package javaSE07_面向对象;
public class Test02_this关键字 {
int a =9;
public static void main(String[] args) {
Test02_this关键字 test02 = new Test02_this关键字();
test02.t1();
// this.t1(); 主方法中不能使用this, -- 有static 修饰的地方不能使用this关键字
}
public void t1(){
int a = 5; //局部变量
System.out.println(a); // 5 局部变量优先级更高
System.out.println(this.a); // 9 this 解决同名问题的
// 在 t1中调用 t2方法
// Test02_this关键字 test02 = new Test02_this关键字();
// test02.t2();
// this.t2();// 使用 this关键字 调用本类的方法 this 也可以省略
t2();
}
/**
* 方法中可以调用方法,但不可以定义方法
*/
public void t2(){
System.out.println("t2的方法");
t3();
}
public void t3(){
}
}