* 提供构造方法:定义无参构造方法,和有参构造方法
*/
代码如下:
public class Demo {
public static void main(String[] args) {
Date date1=new Date(1994,5,22);
date1.showInfo();
Date date2=new Date();
date2.year=1995;
date2.month=6;
date2.day=29;
date2.showInfo();
}
}
//日期类:
public class Date {
int year;
int month;
int day;
//构造方法
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public Date(){
}
public void showInfo(){
System.out.println(year+"年"+month+"月"+day+"日");
}
}