请看下面程序中
MyDate newDay=new MyDate(this);
this是什么作用?
class MyDate{
private int day;
private int month;
private int year; //定义三个成员变量;
public MyDate(int day,int month,int year){
this.day=day;
this.month=month;
this.year=year;
} //构造方法;
public MyDate(MyDate date){
this.day=date.day;
this.month=date.month;
this.year=date.year; //将参数Date类中的成员变量赋给MyDate类;
} //构造方法;
public int getDay(){
return day;
}//方法;
public void setDay(int day){
this.day=day;//参数Day赋给此类中的Ddy;
}
public MyDate addDays(int moreDay){
MyDate newDay=new MyDate(this);
newDay.day=newDay.day+moreDay;
return newDay; //返回整个类;
}
public void print(){
System.out.println("My Date: "+year+"-"+month+"-"+day);
}
}
public class TestMyDate{
public static void main(String args[]){
MyDate myBirth=new MyDate(19,11,1987); //利用构造函数初始化;
MyDate next=myBirth.addDays(7);
//addDays()的返回值是类,将其返回值赋给变量next;
next.print();
}
} |