this关键字
- this 要使用在没有static中
- 出现在构造方法中调用其他构造方法(只能在方法的第一行)
class Customer {
String name;
Customer(String name){
this.name = name;
}
public void shop(){
System.out.println(name+"is shopping"); //name = this.name
}
}
class Date {
private int year;
private int month;
private int day;
public Date(int year,int month,int day){
this.day = day;
this.month=month;
this.year=year;
}
public Date(){
this(2022,3,16); //无参构造调用有参构造,new Date(2022.3.16)会生成一个新对象,
}