//設計一個簡單的日期類Date,然后編寫一個主程序演示該類的用法,日期有不同的顯示格式,
//例如設yyyy、mm、dd分別表示年、月、日,則我們常用的格式是yyyy.mm.dd,美國常用的是
//mm/dd/yyyy,歐洲常用的格式是dd-mm-yyyy.通常同一個程序中的多個日期對象會采用統一
//的顯示格式.所以格式屬性應該定義一個靜態數據成員.
/*
*auther starshus
*
*Date 04/11/20
*/
//5.8.2
public class Date
{
PRivate int year = 2000,month = 1,day=1;//定義并初始化變量
private String greet = "The date is : ";//描述日期的字符串
private static String format = ".";//顯示格式,靜態變量
public void setDate(int year,int month,int day)//設置日期
{
this.year = year;
this.month = month;
this.day = day;
}
public void setGreet(String greet)//設置描述日期的字符串
{
this.greet = greet;
}
public static void setFormat(String note)//設置格式,也應該為靜態方法
{
format = note;
}
public String getDate()//返回日期
{
return(greet+year+format+month+format+day);
}
public static void main(String[] args)//演示類用法的主方法
{
Date date = new Date();//新建對象
date.setDate(2004,10,28);
System.out.println(date.getDate());
Date.format = "/";//設置格式
Date usaDate = new Date();
usaDate.setDate(2004,11,13);
usaDate.setGreet("This is u.s.a format : ");
System.out.println(usaDate.getDate());
Date.format = "-";//設置格式
Date europeDate = new Date();
europeDate.setDate(2004,11,13);
europeDate.setGreet("This is EuropeDate : ");
System.out.println(europeDate.getDate());
}
}
新聞熱點
疑難解答