这是thinking in java上的一个例子:public final class Month { private 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 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]); } } ///:~
看不到的 因为1不仅仅对应的是Font,而且也没有这样的转换源码是这样的,你可以看到BOLD对应1,CENTER_BASELINE也对应1 你觉得1对应的是BOLD还是CENTER_BASELINE呢?/** * The plain style constant. */ public static final int PLAIN = 0; /** * The bold style constant. This can be combined with the other style * constants (except PLAIN) for mixed styles. */ public static final int BOLD = 1; /** * The italicized style constant. This can be combined with the other * style constants (except PLAIN) for mixed styles. */ public static final int ITALIC = 2; /** * The baseline used in most Roman scripts when laying out text. */ public static final int ROMAN_BASELINE = 0; /** * The baseline used in ideographic scripts like Chinese, Japanese, * and Korean when laying out text. */ public static final int CENTER_BASELINE = 1; /** * The baseline used in Devanigiri and similar scripts when laying * out text. */ public static final int HANGING_BASELINE = 2;
勘误: 上面第二句Font改成Font.BOLD
在font里加一个方法public Font int2Font(int f){ return ... }
首先得确认你的BOLD是一个一般常量还是一个枚举值,这是有区别的. Enum类型是jdk5.0以后加入的新特性,下面有我的部分实例代码package net.oicp.sunflowerbbs; enum Operate { push(1), pop(3),clear(5); private int value; Operate( int arg1) { value=arg1; } public int getValue() { return value; } public Operate intToOperate(int a) { Operate result=null; for(Operate o:Operate.values()) if(o.getValue()==a) { result=o; break; } return result; } }public class MyEnum {
private 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 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]);
}
} ///:~
这句是把Font.BOLD转型成int,而不是Font,
所以你那句System.out.println((Font)1)是不对的,要转也是转成BOLD的类型
因为1不仅仅对应的是Font,而且也没有这样的转换源码是这样的,你可以看到BOLD对应1,CENTER_BASELINE也对应1
你觉得1对应的是BOLD还是CENTER_BASELINE呢?/**
* The plain style constant.
*/
public static final int PLAIN = 0; /**
* The bold style constant. This can be combined with the other style
* constants (except PLAIN) for mixed styles.
*/
public static final int BOLD = 1; /**
* The italicized style constant. This can be combined with the other
* style constants (except PLAIN) for mixed styles.
*/
public static final int ITALIC = 2; /**
* The baseline used in most Roman scripts when laying out text.
*/
public static final int ROMAN_BASELINE = 0; /**
* The baseline used in ideographic scripts like Chinese, Japanese,
* and Korean when laying out text.
*/
public static final int CENTER_BASELINE = 1; /**
* The baseline used in Devanigiri and similar scripts when laying
* out text.
*/
public static final int HANGING_BASELINE = 2;
上面第二句Font改成Font.BOLD
return ...
}
Enum类型是jdk5.0以后加入的新特性,下面有我的部分实例代码package net.oicp.sunflowerbbs;
enum Operate {
push(1), pop(3),clear(5);
private int value;
Operate( int arg1) {
value=arg1;
}
public int getValue() {
return value;
}
public Operate intToOperate(int a)
{
Operate result=null;
for(Operate o:Operate.values())
if(o.getValue()==a) {
result=o;
break;
}
return result;
}
}public class MyEnum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*赋值方式一*/
Operate op=Operate.clear;
/*赋值方式二*/
op=Operate.valueOf("push");
/*取得序号值(在集合中的index值) */
System.out.println(op.ordinal());
/*取得其值*/
System.out.println(op.getValue());
/*取得其在集合中的名称 */
System.out.println(op.toString());
/* 通过序号找值*/
Operate[] op2=Operate.values();
System.out.println(op2[1].getValue());
/* 通过其名称找值 */
System.out.println(Operate.valueOf("clear").getValue());
/*通过值找其名称*/
System.out.println(op.intToOperate(5));
}}