前言:简述 Java 中的 this 关键字,涉及 this 的在各类方法中的使用、省略等知识
this 是 Java 中的关键字,存储在堆内存中的某个对象的内存地址(引用),表示当前对象。
this 可以存在于实例方法中,表示当前对象;不能存在于静态方法中调用属性,否则报错无法从静态上下文中引用非静态变量。;存在于构造方法第一行, this()
表示当前构造方法调用本类其他构造方法。
this 大部分情况可以省略,但用于区分局部变量和实例变量时不能省略。
代码实例:this.
this
仅用于实例方法中,哪个对象调用实例方法/属性,this就是谁。
public class ThisTest01{
public static void main(String[] args){
Customer c1 = new Customer("张三");
c1.shopping();
Customer c2 = new Customer("李四");
c2.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String s){ name = s; }
public void shopping(){
System.out.println(name + "正在购物!");
/*
调用 name 属性时,省略了 this.,表示当前对象
当对象 c1 调用 shopping(),this 表示 c1
当对象 c2 调用 shopping(),this 表示 c2
System.out.println(this.name + "正在购物!");
*/
}
}
this的省略
- 不可省略的情况:
- 在实例方法或构造方法中,区分局部变量和实例变量时
- 可省略的情况:
- 在实例方法中访问当前对象的实例变量
- 在
getter
方法中,返回值return (this.)值;
语句中的this.
可省
代码实例:this 是否省略的三种情况
public class ThisTest{
public static void main(String[] args){
Student s = new Student();
s.setNo(111);
s.setName("张三");
System.out.println("学号:" + s.getNo());
System.out.println("姓名:" + s.getName());
s.shopping();
}
}
class Student{
private int no; // 学号
private String name; // 姓名
public Student(){}
public Student(int i, String s){
no = i;
name = s;
}
public void shopping(){
System.out.println(name + "正在购物!");
System.out.println(this.name + "正在购物!"); // 可省略 this
}
public void setNo(int i){ no = i; } // setNo()写法一
public void setNo(int no){ no = no; } // setNo()写法二
public void setNo(int no){ this.no = no; } // setNo()写法三
public int getNo(){ return no; }
public void setName(String s){ name = s; }
// 实例变量name的访问必须是通过“引用.”的方式,因此这里的name前要有"引用.",如果省略了就是“this.”
public String getName(){ return name; }
}
比较三种 setNo()
的写法,写法一、三正确,写法二中两个 no
都是局部变量 no
,和实例变量 no
没关系,实例变量 no
未被重新赋值。另外,不推荐写法一,因为形参变量名无意义。
this()
当前构造方法调用本类其他的构造方法,可在构造方法第一行编写以下语法:
this(构造方法的实际参数列表);
对于 this()
的调用只能出现在构造方法中的第一行,否则报错–Call to 'this()' must be first statement in constructor body
代码实例:this()使用
需求:
- 定义一个日期类,可以表示年月日信息。
- 要求:如果调用无参数构造方法,默认创建的日期为:1970年1月1日;除了调用无参数构造方法之外,也可以调用有参数的构造方法来创建日期对象。
- 在 main 方法中创建两个对象,并输出日期:1970年1月1日、2008年8月8日(用有参构造方法创建对象并输出)
public class ThisTest{
public static void main(String[] args){
// 调用无参数构造方法
Date d1 = new Date();
d1.detail();
// 调用有参数构造方法
Date d2 = new Date(2008, 8, 8);
d2.detail();
}
}
/**
需求:
1、定义一个日期类,可以表示年月日信息。
2、要求:
如果调用无参数构造方法,默认创建的日期为:1970年1月1日。
除了调用无参数构造方法之外,也可以调用有参数的构造方法来创建日期对象。
*/
class Date{
private int year;
private int month;
private int day;
/** 调用无参数构造方法,默认创建的日期为:1970年1月1日*/
public Date(){
//this.year = 1970;
//this.month = 1;
//this.day = 1;
// 以上代码太复杂,简化如下
this(1970, 1, 1);
}
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
/** 打印日期的方法*/
public void detail(){
//System.out.println(year + "年" + month + "月" + day + "日");或以下方式
System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
}
/**setter and getter*/
public void setYear(int year){ this.year = year; }
public int getYear(){ return year; }
public void setMonth(int month){ this.month = month; }
public int getMonth(){ return month; }
public void setDay(int day){ this.day = day; }
public int getDay(){ return day; }
}