Java语言 this 总结

this 小结

Java中的 this 用来指向调用一个方法或属性时,调用该方法或属性的对象;为了方便理解,我们从例子入手探讨 this 用法;

1   public class Student {
2	  String name;
3	  void doHomework(){
4		  System.out.println(this.name+"正在做作业.......");
5	  }
6	  void speak(){
7		  new Student().doHomework();   //this.doHomework();
8		  System.out.println(name+"正在说话......");
9	  }
10	  public static void main(String[] args) {
11		  Student student = new Student();
12		  student.speak();
13	  }
14  }


null正在做作业.......
null正在说话......

这里的 name 虽然都是 null,但请注意,这里的两个 null 不是同一个对象的 null;执行 main()方法;创建一个对象并用 student 指向此对象,执行此对象的 speak()方法;在speak()方法中又创建了一个新的对象,但是并没有变量指向它;并调用了这个没有变量指向的对象的 doHomework()方法,所以第4行的 this 是指这个没有变量指向的对象;而第8行的 this 是指向这个 student 变量指向的对象;如果第7行换成 this.doHomework();则此时第4行的 this 也是指向这个 student 变量指向的对象;

1  public class Student {
2	  String name;
3	  void doHomework(){
4		  System.out.println(this.name+"正在做作业.......");
5	  }
6	  public static void main(String[] args) {
7		  Student student = new Student();
8		  student.name="王五";
9		  student.doHomework();
10	  }
11 }


王五正在做作业.......

很显然,这个例子中的 this 是指这个 name 为“王五”的对象;

1  public class Student {
2	  String name;
3	  void doHomework(){
4		  System.out.println(this.name+"正在做作业.......");
5		  name="刘颖";  //String name="刘颖";
6	  }
7	  void speak(){
8		  System.out.println(name+"正在说话......");
9	  }
10	  public static void main(String[] args) {
11		  Student student = new Student();
12		  student.doHomework();
13		  student.speak();
14	  }
15  }


null正在做作业.......
刘颖正在说话......

调用 doHomework()方法时,student 指向的对象的 name 属性还没有赋值,故此时输出 this.name 自然为 null;speak()方法中输出的 this.name 为 刘颖 是因为在 doHomework()方法中对该对象的 name 属性进行了赋值,使其等于 刘颖;其实,这里的 name 相当于 this.name;是全局变量;故此时的 name 赋值是在给变量 student 指向的对象的 name 赋值;因此 speak()方法输出的是 刘颖正在说话......。

注意:只有在局部变量有变量名和全局变量的变量名相同时,this 不可省略,用来指对象的的变量(即全局变量);而不加 this 的变量则指局部变量。

1  public class Student {
2
3	  int age;
4	  String name;
5
6	  public Student(int age) {
7		  this.age = age;
8	  }
9	  public Student(String name) {
10		  this.name = name;
11	  }
12	  public Student(int age, String name) {
13	  	  this(age);
14		  new Student(name);
15	  }
16	  public static void main(String[] args) {
17		  new Student(12, "王五");
18	  }
19  }

紧接着我们分析下这个例子;第17行创建对象,有参构造方法调用,执行第12-15行的代码,第13行 this (这里也是指向刚创建的对象)调用同类中其他的构造方法,故调用第6-8行代码进行 age 的赋值,使其值为12;紧接着第14行代码创建了一个新的对象并调用9-11行代码的构造方法进行新的对象的 name属性 的赋值,使其值为“王五”;因此我们不难看出其实第17行创建对象并调用有参构造方法的过程中,一共产生了两个对象,一个是 name 为 null,但是其 age 的值为12;另一个是 name 为 “王五”,但是其 age 的值为0;

public class Student {

	String name;

	public static void print() {
		System.out.println(this.name);  //这里的this用法是错误的
	}
	public static void main(String[] args) {
		print();
	}
}

this 是用来指代调用一个方法或属性的对象,而不能指向类;因此在 static 定义的方法(即类的方法)中不能使用 this 调用属性和方法,因为 static 定义的属性和方法一般推荐使用类名引用,而 this 不能指代类。

注意:因此Java语言硬性规定 this 不能用于 static 修饰(即静态)的方法中。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值