求解??

GHK42606 2011-09-17 08:20:15
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();



...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
daijope 2011-09-18
  • 打赏
  • 举报
回复
d1.tomorrow()返回的是一个对象吧,
有一种编程风格叫方法链
打油的程序员 2011-09-17
  • 打赏
  • 举报
回复
d1.tomorrow().outDate(); 由d1指向的Exam3_6对象 调用tomorrow()方法创建 Exam3_6类型的新对象 这个对象的引用(因为你没用在别的地方再次使用它,你没有写出来) 有调用outDate()方法执行打印操作

如果你想别的地方使用那个引用,你不应该简写,而应该写成这样:

Exam3_6 tempExam3_6 = d1.tomorrow();
tempExam3_6.outDate();//


以后你会见到更多的在类内部用函数创建同类型的对象 , 比如getInstance(),createXXX等等,熟悉了
就不会大惊小怪了,还有一点,这些方法通常是静态方法,所以我不太喜欢你上面的写法
gyf4817 2011-09-17
  • 打赏
  • 举报
回复
输出结果分别为:
2000/1/2
2004/1/9

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧