this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。
- public class Date {
- public int year;
- public int month;
- public int day;
- public void setDay(int year, int month, int day){
- this.year = year;
- this.month = month;
- this.day = day;
- }
- public void printDate(){
- System.out.println(this.year + "/" + this.month + "/" + this.day);
- }
- }
注意:this引用的是调用成员方法的对象
特别是在成员方法中形参名和成员变量(属性)的名字一样的时候一定要用this引用,否则该成员方法(行为)将与成员变量(属性)无关。因为由于局部变量优先原则,该行为中的参数已经与属性无关,所以属性的值都是默认值,并没有得到赋值。
该用法也是最为普遍的一种用法,在上面的代码中也已经展示过如何使用。大部分时候,普通方法访问其他方法、成员变量时无须使用 this 前缀,但如果方法里有个局部变量和成员变量同名,但程序又需要在该方法里访问这个被覆盖的成员变量,则必须使用 this 前缀。
- public void setDay(int year, int month, int day){
- this.year = year;
- this.month = month;
- this.day = day;
- }
this.
year=
year语句表示一个赋值语句,等号左边的this.
year是指当前对象具有的变量year,等号右边的year表示参数传递过来的数值。
提示:当一个类的属性(成员变量)名与访问该属性的方法参数名相同时,则需要使用 this 关键字来访问类中的属性,以区分类的属性和方法中的参数。
this 关键字最大的作用就是让类中一个方法,访问该类里的另一个方法或实例变量。
这是一个Dog类
- public class Test {
- public static void main(String[] args) {
- Dog dog=new Dog();
- dog.setDog("旺财","白色");
- dog.wag();
- dog.bark();
-
- }
- }
- class Dog{
-
- public String name;
- public String color;
-
-
- public void bark(){
- System.out.println(name+"汪汪叫");
- }
- public void wag(){
- System.out.println(name+"摇尾巴");
- }
-
- public void setDog(String name,String color){
- this.name=name;
- this.color=color;
- }
- }
那么如何在一个方法里面调用另一个方法呢?
- public class Test {
- public static void main(String[] args) {
- Dog dog=new Dog();
- dog.setDog("旺财","白色");
- dog.wag();
- // dog.bark();
-
- }
- }
- class Dog{
-
- public String name;
- public String color;
-
-
- public void bark(){
-
- System.out.println(name+"汪汪叫");
-
- }
- public void wag(){
- this.bark();
- System.out.println(name+"摇尾巴");
-
- }
-
- public void setDog(String name,String color){
- this.name=name;
- this.color=color;
- }
- }
当把this引用去除之后改成这样:
- public void wag(){
- bark();
- //this.bark();
- System.out.println(name+"摇尾巴");
-
- }
也是可以实现的。
大部分时候,一个方法访问该类中定义的其他方法、成员变量时加不加 this 前缀的效果是完全一样的。省略 this 前缀只是一种假象,虽然程序员省略了调用 bark() 方法之前的 this,但实际上这个 this 依然是存在的。
注意:对于 static 修饰的方法而言,可以使用类来直接调用该方法,如果在 static 修饰的方法中使用 this 关键字,则这个关键字就无法指向合适的对象。所以,static 修饰的方法中不能使用 this 引用。并且 Java 语法规定,静态成员不能直接访问非静态成员。
this( ) 用来访问本类的构造方法(构造方法是类的一种特殊方法,方法名称和类名相同,没有返回值,且有无参构造方法和有参构造方法)
下面定义一个 Student 类,使用 this( ) 调用构造方法给 name 赋值,Student 类的代码如下所示:
-
- public class Student {
- String name;
- // 无参构造方法(没有参数的构造方法)
- public Student() {
- this("张三");
- }
- // 有参构造方法
- public Student(String name) {
- this.name = name;
- }
- // 输出name和age
- public void print() {
- System.out.println("姓名:" + name);
- }
- public static void main(String[] args) {
- Student stu = new Student();
- stu.print();
- }
- }
注意:
this()不能在普通方法中使用,只能在构造方法中使用。
this()在构造方法中的时候必须是第一条语句。
1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
2. this只能在"成员方法"中使用
3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象
4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责来接收
如最上面的Date类:
- public class Date {
- public int year;
- public int month;
- public int day;
- public void setDay(Date this,int year, int month, int day){
- this.year = year;
- this.month = month;
- this.day = day;
- }
- public void printDate(Date this){
- System.out.println(this.year + "/" + this.month + "/" + this.day);
- }
- }
这样也是可以编译通过的。