Date days[] = {newDate(1,3,7),newDate(2,3,6),newDate(3,4,7) };
for(inti =0; i < days.length; i++) {
System.out.println(days[i]);
}
}
}
classDate {
intyear;
intmonth;
intday;
Date(inty,intm,intd) {
year = y;
month = m;
day = d;
}
publicString toString() {
returnyear +"年"+ month +"月"+ day +"日";
}
}
复制代码
作者: 聂益飞 时间: 2013-3-17 12:13
public class Test{
public static void main(String[] args){
Date days[] = {
new Date(1,3,7),
new Date(2,3,6),
new Date(3,4,7)
};
for(int i = 0;i<days.length;i++){
System.out.println(days[i]); //你这里打印的是数组对象,当然打印的是以[@开头的地址值
}
}
public String toString(){ //不是重写这里的toString
return "qwe";
}
}
class Date{ //重写这里的toString便可,返回值类型String
int year; int month;int day;
Date(int y,int m, int d){
year = y;
month = m;
day = d;