免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java java enum
最近访问板块 发新帖
查看: 1817 | 回复: 1
打印 上一主题 下一主题

java enum [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-02 16:48 |只看该作者 |倒序浏览
java enum
  1. 1 package com.karl.test;
  2. 2
  3. 3 public class TestEnum {
  4. 4     
  5. 5     public enum ColorSelect{
  6. 6         red,green,yellow,blue;
  7. 7     }
  8. 8
  9. 9     public enum Season{
  10. 10         spring,summer,fall, winter;
  11. 11         private final static String location = "Phoenix";
  12. 12         
  13. 13         public static Season getBest(){
  14. 14             if(location.endsWith("Phoenix"))
  15. 15                 return winter;
  16. 16             else
  17. 17                 return summer;
  18. 18         }
  19. 19     }
  20. 20     
  21. 21     public enum Temp{
  22. 22         absoluteZero(-459), freezing(32), boiling(212), paperBurns(451);
  23. 23         private final int value;
  24. 24         public int getValue(){
  25. 25             return value;
  26. 26         }
  27. 27         Temp(int value){
  28. 28             this.value = value;
  29. 29         }
  30. 30     }
  31. 31     
  32. 32     
  33. 33     public static void main(String[] args) {
  34. 34         ColorSelect m = ColorSelect.blue;
  35. 35         switch(m){
  36. 36         case red:
  37. 37             System.out.println("color is red");
  38. 38             break;
  39. 39         case green:
  40. 40             System.out.println("color is green");
  41. 41             break;
  42. 42         case yellow:
  43. 43             System.out.println("color is yellow");
  44. 44             break;
  45. 45         case blue:
  46. 46             System.out.println("color is blue");
  47. 47             break;
  48. 48         }
  49. 49         
  50. 50         for(ColorSelect c : ColorSelect.values()){
  51. 51             System.out.println(c);
  52. 52         }
  53. 53         
  54. 54         System.out.println("total " + ColorSelect.values().length + " in ColorSelect.");
  55. 55         
  56. 56         System.out.println("the position of blue in ColorSelect is " + ColorSelect.blue.ordinal());
  57. 57         
  58. 58         System.out.println("compare red to green " + ColorSelect.red.compareTo(ColorSelect.green));
  59. 59         
  60. 60         System.out.println("Season.getBest()= " + Season.getBest());
  61. 61         
  62. 62         for (Temp t : Temp.values()) {
  63. 63             System.out.println("The value of t is " + t.getValue());
  64. 64         }
  65. 65
  66. 66     }
  67. 67 }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-02 22:43 |只看该作者
路过  
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP