【JavaSE】this引用

一、this引用存在的意义

首先我们先来看一个例子

public class Date {
    public int year;
    public int month;
    public int day;

    public void setDay(int y,int m,int d){
        year = y;
        month = m;
        day = d;
    }
    public void printDate(){
        System.out.println(year + "/" + month + "/" + day);
    }

    public static void main(String[] args) {
        Date d1 = new Date();
        Date d2 = new Date();
        Date d3 = new Date();

        d1.setDay(2021,5,12);
        d2.setDay(2022,10,25);
        d3.setDay(2023,4,12);

        d1.printDate();
        d2.printDate();
        d3.printDate();
    }
}

 此时这个类没有任何的问题,也可以正常运行,但是我们在敲代码时会出现一些意想不到的问题

  • 形参名不小心与成员变量名相同

此时我们仅改变形参名,不改变上述代码的其他地方

 我们通过运行代码可以知道,我们通过Date类中的成员变量对对象进行的设置并没有打印出来,而是直接打印了默认的0值

主要是因为这样无法分清到底是谁给谁赋值

成员变量给成员变量?参数给参数?参数给成员变量?成员变量给参数?

  •  我们可以看到三个对象都在调用setDay()和printDate(),但是两个函数中并没有关于对象的说明,他们怎么知道打印的是哪个对象的数据呢?

此时我们就需要用到this了

二、何为this引用 

this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。 

像上面第一个问题我们就可以通过this引用来解决

注:

       this引用的是调用成员方法的对象

三、this引用的用法  

1.this.成员变量 

一般情况下,普通方法访问其他方法、成员变量时无须使用 this 前缀,但如果方法里形参与成员变量同名,但程序又需要在该方法里访问这个被覆盖的成员变量,则必须使用 this 前缀。

public void setDay(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
}

2.this.成员方法 

使类中一个方法,访问该类里的另一个方法或实例变量

class Dog{
    public String name;
    public String color;

    public void bark(){
        System.out.println(name + "汪汪叫");
    }
    public void show(){
        this.bark();
        System.out.println(name + "摇尾巴");
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "小美";
        dog.color = "白色";
        dog.show();
    }
}

大部分时候,一个方法访问该类中定义的其他方法、成员变量时加不加 this 前缀的效果是完全一样的。虽然省略了调用 bark() 方法之前的 this,但实际上这个 this 依然是存在的。

class Dog{
    public String name;
    public String color;

    public void bark(){
        System.out.println(name + "汪汪叫");
    }
    public void show(){
        bark();
        System.out.println(name + "摇尾巴");
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "小美";
        dog.color = "白色";
        dog.show();
    }
}

3.this()   访问构造方法 

public class Person {
    public String name;
    public int age;

    public Person(){
        this("李四",19);
        System.out.println("不带参数的构造方法");
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("带参数的构造方法");
    }

    public void show(){
        System.out.println("姓名:" + name + " " + "年龄:" + age);
    }
    public static void main(String[] args) {
        Person person = new Person();
        person.show();
    }
}

 

注:

  • this()不能在普通方法中使用,只能在构造方法中使用。
  • this()在构造方法中的时候必须是第一条语句。

四、this引用的特性 

  1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
  2. this只能在"成员方法"中使用
  3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象
  4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责来接收 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨染无尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值