public final class Month { private static String name;
private Month(String nm){name=nm;}
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 n){
return month[n-1];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<12;i++)
{
System.out.println(Month.month[i]);
}
Month m=Month.JAN;
System.out.println(m);
m=Month.number(11);
System.out.println(m);
System.out.println(m==Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}}这个程序的输出是:
December
December
December
December
December
December
December
December
December
December
December
December
December
December
false
false
December
这里对JAN,FEB等的赋值有没有什么问题?
还有这里的相等和equals测试,是比较reference还是比较reference所指向的对象,按照
输出结果,貌似是比较reference本身
private Month(String nm){name=nm;}
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 n){
return month[n-1];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<12;i++)
{
System.out.println(Month.month[i]);
}
Month m=Month.JAN;
System.out.println(m);
m=Month.number(11);
System.out.println(m);
System.out.println(m==Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}}这个程序的输出是:
December
December
December
December
December
December
December
December
December
December
December
December
December
December
false
false
December
这里对JAN,FEB等的赋值有没有什么问题?
还有这里的相等和equals测试,是比较reference还是比较reference所指向的对象,按照
输出结果,貌似是比较reference本身
if(!obj instanceof Month)
return false;
else
return this.name.equals(((Month)obj).getName());
}
public String getName(){
return this.name;
}
这条语句
=>
private String name;
name = nm;
} 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 n) {
return month[n - 1];
} public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 12; i++) {
System.out.println(Month.month[i]);
}
Month m = Month.JAN;
System.out.println(m);
m = Month.number(11);
System.out.println(m);
System.out.println(m == Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}}
实例化再多也只剩最后一个
输出结果,貌似是比较reference本身>
是reference!
自己写的类默认是比较对象的引用,要重写equals()方法!
java自带的类,大多数是重写过的!