1. 程式人生 > >重寫enum的valueof方法等

重寫enum的valueof方法等

enum 物件的常用方法介紹

int compareTo(E o) 
          比較此列舉與指定物件的順序。

Class<E> getDeclaringClass() 
          返回與此列舉常量的列舉型別相對應的 Class 物件。

String name() 
          返回此列舉常量的名稱,在其列舉宣告中對其進行宣告。

int ordinal() 
          返回列舉常量的序數(它在列舉宣告中的位置,其中初始常量序數為零)。

String toString()

           返回列舉常量的名稱,它包含在宣告中。

static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)

 
          返回帶指定名稱的指定列舉型別的列舉常量。

複製程式碼

package test;

public class EnumTest {
    public enum Color {
        RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(0, 255, 0);
                                                         // 構造列舉值,比如RED(255,0,0)
        private Color(int rv, int gv, int bv) {
            this.redValue = rv;
            this.greenValue = gv;
            this.blueValue = bv;
        }


        private int redValue;                             // 自定義資料域,private為了封裝。
        private int greenValue;
        private int blueValue;
        
        public static final Color[] values=Color.values();
        public static Color valueOf(int i) {
            return values[i];
        }
        

        public String toString() {                         // 覆蓋了父類Enum的toString()
            return super.toString() + "(" + redValue + "," + greenValue + "," + blueValue + ")";
        }
    }
    
    public enum ColorType{
        Red(Color.RED),
        Blue(Color.BLUE),
        Black(Color.BLACK),
        Yellow(Color.YELLOW),
        Green(Color.GREEN);
        
        private Color colorId;
        private ColorType(Color colorId) {
            this.colorId=colorId;
        }
        
        public static ColorType[] a=ColorType.values();
        public static ColorType valueOf(int i) {
            return a[i];
        }
        
        public String toString() {
            return super.toString()+"-------------->"+colorId.toString();
        }
        
    }

    public static void main(String args[]) {
        // Color colors=new Color(100,200,300); //wrong
        Color color = Color.RED;
        Color colorYellow=Color.YELLOW;
        System.out.println(color);                         // 呼叫了toString()方法
        System.out.println(color.ordinal());
        System.out.println(color.compareTo(colorYellow));  //返回的是兩個列舉值的順序之差
        System.out.println(Color.valueOf("BLUE"));
        System.out.println(Color.valueOf(1));              //重寫valueOf方法  
        
        System.out.println(ColorType.valueOf(2).toString());
    }

}

複製程式碼

 

執行結果:

RED(255,0,0)
0
-3
BLUE(0,0,255)
BLUE(0,0,255)
Black-------------->BLACK(0,0,0)

 


 

自定義方法:

複製程式碼

package test;

public class EnumTest3 {
    public enum EnumTest {
        MON(1), TUE(2), WED(3), THU(4), FRI(5), SAT(6) {
            @Override
            public boolean isRest() {
                return true;
            }
        },
        SUN(0) {
            @Override
            public boolean isRest() {
                return true;
            }
        };
        
        private int value;
        
        private EnumTest(int value) {
            this.value = value;
        }
        
        public int getValue() {
            return value;
        }
        
        public boolean isRest() {
            return false;
        }
    }
    public static void main(String[] args) {
        System.out.println("EnumTest.FRI 的 value = " + EnumTest.SAT.isRest());
    }
}

複製程式碼

輸出結果:

EnumTest.F