关于thinking in java 中month的问题
小弟自学Thinking in java看到接口这章的这个程序
public final class Month {
private String name;
private Month(String nm){
name=nm;//nm=name;
}
public String toString(){
return name;
}
public static final Month
JAN=new Month( "JANUARY "),
FEB=new Month( "FEBRUARY "),
MAR=new Month( "MARCH "),
APR=new Month( "APRIL "),
MAY=new Month( "MAY "),
JUN=new Month( "JUNE "),
JUL=new Month( "JULY "),
AUG=new Month( "AUGUST "),
SEP=new Month( "SEPTEMBER "),
OCT=new Month( "OCTOBER "),
NOV=new Month( "NOVEMBER "),
DEC=new Month( "DECEMBER ");
public static final Month[] month={
JAN,FEB,MAR,APR,MAY,JUN,
JUL,AUG,SEP,OCT,NOV,DEC
};
public static final Month number(int ord){
return month[ord -1];
}
public static void main(String[] args) {
Month m=Month.JAN;
System.out.println(m);
m=Month.number(12);
System.out.println(m);
System.out.println(m==Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}
}
其中name=nm为什么不能写成nm=name,还有最后的number方法中有(ord-1)
为什么Month.number(12)输出了DECEMBER,Month.month[3]为什么输出的是APRIL而不是MARCH?
------解决方案--------------------Month.number(12) = month[11]DECEMBER,Month.month[3]APRIL
month[0]就是JANUARY
------解决方案--------------------其中name=nm为什么不能写成nm=name
-------
这个。。。。
你给形参赋值有什么用
------解决方案--------------------1、java中的赋值语句就这样的.
2、public static final Month[] month={
JAN,FEB,MAR,APR,MAY,JUN,
JUL,AUG,SEP,OCT,NOV,DEC
};
Month.moth[3]=APR;