关于Java|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) {

	// 构造三个日期类型的对象 d1 d2 d3
	Date d1 = new Date();
	Date d2 = new Date();
	Date d3 = new Date();
	
	// 对d1,d2,d3的日期设置
	d1.setDay(2020,9,15);
	d2.setDay(2020,9,16);
	d3.setDay(2020,9,17);
	
	// 打印日期中的内容
	d1.printDate();
	d2.printDate();
	d3.printDate();
	}
}

运行结果如下
在这里插入图片描述

  • 以上代码定义了一个日期类,然后main方法中创建了三个对象,并通过Date类中的成员方法对对象进行设置和打印。

此时将setDay改为

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

运行结果如下
在这里插入图片描述

  • 在上述代码中,setDay形参名与成员变量名相同,三个对象都在调用setDate和printDate方法,但是这两个函数中没有任何有关对象的说明,setDay和printDate并不知道打印的是哪个对象的数据
    并且,由于“同名情况下,局部变量的优先级更高”原则,在构造方法执行的过程中,虚拟机会把参数值赋给参数本身,而不是赋值给对象的属性,所以我们所期望的参数值最终没有被赋值到对象属性中
  • 此时我们就需要用到 this.成员变量 访问当前对象的实例成员变量

首先,我们需要知道每个成员方法的第一个参数,默认是this
在这里插入图片描述

  • 此时this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型

我们都上述代码作出如下更改

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

运行结果如下
在这里插入图片描述

  • 此时,this关键字调用成员方法的对象,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法,对象的引用传递给该成员方法,this负责来接收

总结及注意点

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

this.成员方法 访问当前对象的实例成员方法

实例分析
在上述setDay中作出更改

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

运行结果如下
在这里插入图片描述

  • 上述代码在setDay方法中调用了printDate方法

this(参数列表) 访问当前对象的构造方法

实例分析

public class Date {

    public int year;
    public int month;
    public int day;

    // 构造方法:
    public Date(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("Date(int,int,int)方法被调用了");
    }

    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }

    public static void main(String[] args) {
        Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了
        d.printDate(); // 2021-6-9
    }
}

运行结果如下
在这里插入图片描述

  • 上述代码中 this( ) 用来访问本类的构造方法(构造方法是类的一种特殊方法,方法名称和类名相同,没有返回值。除此之外,这种 this(参数)方式只能在 构造方法 中使用 ,不能在普通方法中使用。如果在普通方法中使用,将会被视为语法错误。

通过this调用其他构造方法来简化代码

public class Date {

    public int year;
    public int month;
    public int day;

    // 无参构造方法--内部给各个成员赋值初始值,该部分功能与三个参数的构造方法重复
    // 此处可以在无参构造方法中通过this调用带有三个参数的构造方法
    public Date(){
        this(1900, 1, 1);
        //等同于
        //this.year = 1900;
        //this.month = 1;
        //this.day = 1;
    }
    
    // 带有三个参数的构造方法
    public Date(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }

    public static void main(String[] args) {
        Date d = new Date();
        d.printDate();
    }
}

运行结果如下
在这里插入图片描述

注意:

  • this(1900,1,1);必须是构造方法中第一条语句
    public Date(){
        System.out.println(year); //注释取消掉,编译会失败
        this(1900, 1, 1);
        //this.year = 1900;
        //this.month = 1;
        //this.day = 1;
    }

报错在这里插入图片描述

  • 不能形成环
public Date(){
	this(1900,1,1);
}
public Date(int year, int month, int day) {
	this();
}
/*
无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用
编译报错:java: 递归构造器调用
*/

报错
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值