this关键字

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(){
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值