this是一个神奇的东西,你经常用它,但是它真正代表的是什么你知道么。如何在合适的位置添加this让我们的代码更好看,下面我们就由浅入深的一探究竟。
首先介绍this的第一种用法:this调用本类中的属性,也就是类中的成员变量;
package com.umbrella.common.model; public class Person { private String name; private int age; private int id; public String getName(){ return this.name; } public int getAge(){ int age = 0; this.age = age; return age; } public int getId(){ int id = 0; id = id; return id; } public void setName(String name){ this.name = name; } public void setAge(int age){ age = age; } public void setId(){ int id = 20; id = id; } public Person() { } public Person(String name, int age, int id) { this.name = name; this.age = age; this.id = id; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + '}'; } public static void main(String[] args){ Person p = new Person("z3",100,444); System.out.println(p.toString()); } }
执行main方法 输出毫无疑问是
Person{name='z3', age=100, id=444}
==================================================================
下面我们更改Main方法
public static void main(String[] args){ Person p = new Person("z3",100,444); p.setAge(20); System.out.println(p.toString()); }
输出结果 Person{name='z3', age=100, id=444}
注意观察 在setAge的时候我定义了方法内的局部变量,所以这个时候 age = age;赋值的并不是类的成员变量。
==================================================================
下面继续改造Main方法
public static void main(String[] args){ Person p = new Person("z3",100,444); p.setId(); System.out.println(p.toString()); }
输出结果:Person{name='z3', age=100, id=444}
这个时候 没有参数的情况下 id仍然没有赋值给成员变量,那么我们怎么办?
==================================================================
继续改造Main方法
public static void main(String[] args){ Person p = new Person("z3",100,444); p.setName("ljh"); System.out.println(p.toString()); }
注意这次setName(name){this.name = name};
增加了this,我们来看输出结果:Person{name='ljh', age=100, id=444}
可以看出成员变量终于变了!
这说明 this.XXX 代表了XXX所属类的成员变量的名字
当我们在方法中声明一个变量时,调用this指向newId 显然是有问题的,因为该类中没有newId成员变量,而方法不属于一个类,所以它的私有变量不具备被this引用的资格。
==================================================================
巩固一下,我们来执行get方法
public static void main(String[] args){ Person p = new Person("z3",100,444); System.out.println(p.toString()); System.out.println(p.getAge()); System.out.println(p.getId()); System.out.println(p.getName()); System.out.println(p.toString()); }
相应的get方法:
public String getName(){ return this.name; } public int getAge(){ int age = 0; this.age = age; return age; } public int getId(){ int id = 0; id = id; return id; }
输出结果:
Person{name='z3', age=100, id=444}
0
0
z3
Person{name='z3', age=0, id=444}
再次说明 加注this关键字就是表面指向的就是本类中的成员变量。不加的话优先就近局部变量
==================================================================
下面介绍this的第二种用法:this调用本类中的其他方法;
解释:就是在一个类中有许多方法,这些方法都同属于这个类,当一个类想调用另一个类的方法时,可以加上this关键字
this关键字除了可以调用成员变量之外,还可以调用构造方法。在一个Java类中,其方法可以分为成员方法和构造方法两种。
例如:
public class Person { private String name; public void killSomeOne(){ System.out.println("i will kill ljh"); } public Person(String name) { this.name = name; } public Person() { this("ljh"); this.killSomeOne(); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } public static void main(String[] args) { System.out.println(new Person()); } }
可以看到idea自动的帮我们打印出name:ljh
说明直接调用了这个类中的含参构造器 Person(String name)。我们看一下输出:
i will kill ljh
Person{name='ljh'}
如果不加this,想调用方法,我们就得:
Person p = new Person("ljh"); p.killSomeOne();
如果一个类中有多个构造方法,因为其名字都相同,跟类名一致,那么这个this到底是调用哪个构造方法呢?其实,这跟采用其他方法引用构造方法一样,都是通过形式参数来调用构造方法的。如上例中,this关键字后面加上了一个参数,那么就表示其引用的是带参数的构造方法。如果现在有三个构造方法,分别为不带参数、带一个参数、带两个参数。那么Java编译器会根据所传递的参数数量的不同,来判断该调用哪个构造方法。
注意
如果无参构造器中this调用构造器不出现在第一行 会报错。
==================================================================
下面介绍this的第三种用法:this返回对象的值;
public class Person { private String name; public Person getPersonInstance(){ return this; } public Person(String name) { this.name = name; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } public static void main(String[] args) { Person p = new Person("ljh"); System.out.println(p.getPersonInstance()); } }
其中 getPersonInstance方法直接返回this,就一个this,仅仅一个this,我们来看看它返回的是什么:
Person{name='ljh'}
可以看出它返回了这个类的对象!
==================================================================
tips
1、不能在类的static成员或static块中使用this。
2、不能在非构造方法中调用this构造方法,只能在构造方法中通过this来调用其他构造方法。
3、不能通过this递归调用构造方法,即不能在一个构造方法中通过this直接或间接调用该构造方法本身
public class Person { Public Person(){ this("ljh"); } Public Person(String name){ this("ljh","dahaoren"); } public Person(String name,String isGoodMan){ this(); } }