java中this关键字的理解

1.我的理解,java中的this主要是为了实现一种在对象未产生之前去使用该对象的成员变量或者方法的功能。只是这种超前的使用只能在本类里面进行而已,就是说,我们可以在一个类中超前使用this关键字,在该类还未实例化之前。如以下代码。

public class TestThisKeyWord {
	private String name="mike";
	private int age=23;
	public TestThisKeyWord(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("name is "+this.name+", and age is "+this.age);
	}
}

上述代码中,构造方法中的this用于区分TestThisKeyWord类中成员变量及构造方法中的局部变量。this在此处指代的是当前的对象。当我们实例化一个TestThisKeyWord对象的时候,this就是这个实例化出来的对象。如:

	public static void main(String[] args) {
		TestThisKeyWord keyword=new TestThisKeyWord("tom",27);
		keyword.info();

	}
当实例化发生的时候,这边的this其实就是keyword这个实力对象了。

上述代码的结果是 name is tom, and age is 27

2.需要记住的是,this出现的代码段,隶属于哪个类,这边的this指代的就是这个被它所隶属的类。比如上述的代码中,info方法中的this,是属于TestThisKeyWord类的,当实例化的时候,它便指代TestThisKeyWord的实例对象。

3.现在我们将TestThisKeyWord中的代码稍作修改,变成下面这样。

我们先新增一个class,如下所示。

public class AnotherClass {
	protected String name = "祥子777";
	protected int age = 26;
}

我们再该类中之定义了两个成员变量,其他什么也没做,同时为了能在TestThisKeyWord中能够访问,我们将它的访问权限设为protected。

接下来我们改变TestThisKeyWord中的代码,如下所示。

public class TestThisKeyWord {
	private String name="mike";
	private int age=23;
	public TestThisKeyWord(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("name is "+this.name+", and age is "+this.age);
		new AnotherClass(){
			private void shout(){
				System.out.println("another class,name is "+this.name+", and age is "+this.age);
			}
		}.shout();
		}
	
}

我在info方法中加了一个匿名类,就是AnotherClass,并且在该匿名类中写了一个shout方法,其实是为了打出this.name和this.age这两个成员变量。运行的结果如下所示。

name is tom, and age is 27
another class,name is 祥子777, and age is 26

由此可见,shout方法中的this指代的类是AnotherClass,而不是外面的TestThisKeyWord这个类。所以,记住,this指代的是它所隶属的那个类。

4.接下来,我们再稍作改动,在shout方法中再添加一个shout2方法,只不过shout2方法拿到外面去写,如下。

public class TestThisKeyWord {
	private String name="mike";
	private int age=23;
	public TestThisKeyWord(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("name is "+this.name+", and age is "+this.age);
		new AnotherClass(){
			private void shout(){
				System.out.println("another class,name is "+this.name+", and age is "+this.age);
				shout2();
			}
		}.shout();
		}
	protected void shout2() {
		System.out.println("here,the name is "+this.name+", and the age is "+this.age);
		
	}
}

这一次打出来的结果是:

name is tom, and age is 27
another class,name is 祥子777, and age is 26
here,the name is tom, and the age is 27

由此可见,在shout2中的this现在指代的是TestThisKeyWord这个类,而不是shout方法所隶属的AnotherClass.

可见,this的指代对象只与代码处在何处有关,我们只需要看this的最近的那个类是什么,它指代的就是什么,shout2外层的最近的类是TestThisKeyWord,所以它指代的是TestThisKeyWord。或者说,因为Shout2是在TestThisKeyWord里面定义的,而在Shout方法中只是调用了这个方法而已。

5.再将代码稍作修改,如下所示。

public class TestThisKeyWord {
	private String name="mike";
	private int age=23;
	public TestThisKeyWord(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void info(){
		System.out.println("name is "+this.name+", and age is "+this.age);
		new AnotherClass(){
			private void shout(){
				System.out.println("another class,name is "+this.name+", and age is "+this.age);
				System.out.println("still TestThisKeyWord class,name is "+TestThisKeyWord.this.name+", and age is "+TestThisKeyWord.this.age);
				shout2();
			}
		}.shout();
		}
	protected void shout2() {
		System.out.println("here,the name is "+this.name+", and the age is "+this.age);
		
	}
}

增加了一句代码,用于调用最外层类的成员变成,打印出的结果是。

name is tom, and age is 27
another class,name is 祥子777, and age is 26
still TestThisKeyWord class,name is tom, and age is 27
here,the name is tom, and the age is 27

由此可见,当在内部类中,我们想访问外部类的成员变量,我只需要在this关键字前加上外部类的名字即可。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值