如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
RED,GREEN,YELLOW; } class Color1{ //模仿枚舉 private static int ordinal=- 1 ; private Color1(){ ordinal++; } public static final Color1 RED= new Color1(); public static final Color1 GREEN= new Color1(); public static final Color1 YELLOW= new Color1(); public int ordinal(){ return ordinal; } } |
在這里我只模仿了獲取枚舉的下標,大家有興趣可以模仿一下其他方法
以上這篇java仿枚舉實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。