求解??
public class Exam3_6
{
private int year ,month,day;
Exam3_6()
{
year=2000;
month=1;
day= 1;
}
Exam3_6(int a,int b,int c)
{
year=a;
month=b;
day=c;
}
Exam3_6(Exam3_6 d){
year=d.year;
month=d.month;
day=d.day;
}
public void outDate()
{
System.out.println(year+"/"+month+"/"+day);
}
public Exam3_6 tomorrow(){
Exam3_6 d=new Exam3_6(this);
d.day++;
if(d.day>d.dayInMonth()){
d.day=1;
d.month++;
if(d.month>12){
d.month=1;
d.year++;
}
}
return d;
}
public int dayInMonth(){
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
return 31;
case 4:case 6:case 9:case 11:
return 30;
default:
if( year %4==0&&year %100!=0||year%400==0)
return 29;
else
return 28;
}
}
public static void main (String arfs[])
{
Exam3_6 d1=new Exam3_6();
System.out.println("The current date is (year/month/day):");
d1.outDate();
System.out.println();
System.out.println("its tomorrow is (year/month/day):") ;
d1.tomorrow().outDate();
System.out.println();
Exam3_6 dd=new Exam3_6(2004,1,8);
System.out.println("The current date is (year/month/day):");
dd.outDate();
System.out.println();
System.out.println("Its tomorrow is (year/month/day):");
dd.tomorrow().outDate();
System.out.println();
}
}
这两句什么意思----》》》
d1.tomorrow().outDate();
dd.tomorrow().outDate();
------解决方案--------------------输出结果分别为:
2000/1/2
2004/1/9
------解决方案--------------------d1.tomorrow().outDate(); 由d1指向的Exam3_6对象 调用tomorrow()方法创建 Exam3_6类型的新对象 这个对象的引用(因为你没用在别的地方再次使用它,你没有写出来) 有调用outDate()方法执行打印操作
如果你想别的地方使用那个引用,你不应该简写,而应该写成这样:
Java code
Exam3_6 tempExam3_6 = d1.tomorrow();
tempExam3_6.outDate();//
------解决方案--------------------
d1.tomorrow()返回的是一个对象吧,
有一种编程风格叫方法链