怎么样使用静态变量?
//设计一个简单的日期类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()); } }