码迷,mamicode.com
首页 > 编程语言 > 详细

java枚举变量反解析用法

时间:2018-05-09 15:13:36      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:get   values   win   lte   mat   结果   stat   static   div   

最近常常有一些项目需要给枚举设值一个int值,以及对int值进行反解析出枚举类型,代码如下:

 1 public enum MatchResultEnum {
 2 
 3     /**
 4      * 赢
 5      */
 6     WIN(0),
 7     /**
 8      * 输
 9      */
10     LOSE(1),
11     /**
12      * 平局
13      */
14     DRAW(2);
15 
16     /**
17      * 比赛结果的code值
18      */
19     private int code;
20 
21     MatchResultEnum(int value) {
22         this.code = value;
23     }
24 
25     public int getCode() {
26         return code;
27     }
28 
29 
30     public static MatchResultEnum parse(int value) {
31         MatchResultEnum[] values = values();
32         for (MatchResultEnum matchResult : values) {
33             if (matchResult.code == value) {
34                 return matchResult;
35             }
36         }
37         return null;
38     }
39 }

 

java枚举变量反解析用法

标签:get   values   win   lte   mat   结果   stat   static   div   

原文地址:https://www.cnblogs.com/zhangshiwen/p/9013347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!