码迷,mamicode.com
首页 > 其他好文 > 详细

常见枚举用法

时间:2018-06-17 19:01:36      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:多继承   class   lang   覆盖   start   枚举   nbsp   相关   ace   

1.常量

把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。 

Java代码 
  1. public enum Color {  
  2.   RED, GREEN, BLANK, YELLOW  
  3. }  

 

2.switch

使用枚举,能让我们的代码可读性更强。 

Java代码 
  1. enum Signal {  
  2.     GREEN, YELLOW, RED  
  3. }  
  4. public class TrafficLight {  
  5.     Signal color = Signal.RED;  
  6.     public void change() {  
  7.         switch (color) {  
  8.         case RED:  
  9.             color = Signal.GREEN;  
  10.             break;  
  11.         case YELLOW:  
  12.             color = Signal.RED;  
  13.             break;  
  14.         case GREEN:  
  15.             color = Signal.YELLOW;  
  16.             break;  
  17.         }  
  18.     }  
  19. }  

 

 

3.向枚举中添加新方法

如果打算自定义自己的方法,那么必须在enum实例序列的最后添加一个分号。而且 Java 要求必须先定义 enum 实例。 

Java代码 
  1. public enum Color {  
  2.     RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);  
  3.     private String name;  
  4.     private int index;  
  5.     private Color(String name, int index) {  
  6.         this.name = name;  
  7.         this.index = index;  
  8.     }  
  9.     public static String getName(int index) {  
  10.         for (Color c : Color.values()) {  
  11.             if (c.getIndex() == index) {  
  12.                 return c.name;  
  13.             }  
  14.         }  
  15.         return null;  
  16.     }  
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     public int getIndex() {  
  24.         return index;  
  25.     }  
  26.     public void setIndex(int index) {  
  27.         this.index = index;  
  28.     }  
  29. }  

 

 

4.覆盖枚举的方法

下面给出一个toString()方法覆盖的例子。 

Java代码 
  1. public enum Color {  
  2.     RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);  
  3.     private String name;  
  4.     private int index;  
  5.     private Color(String name, int index) {  
  6.         this.name = name;  
  7.         this.index = index;  
  8.     }  
  9.     @Override  
  10.     public String toString() {  
  11.         return this.index+"_"+this.name;  
  12.     }  
  13. }  

 

5.实现接口

所有的枚举都继承自java.lang.Enum类。由于Java 不支持多继承,所以枚举对象不能再继承其他类。 

Java代码 
  1. public interface Behaviour {  
  2.     void print();  
  3.     String getInfo();  
  4. }  
  5. public enum Color implements Behaviour{  
  6.     RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);  
  7.     private String name;  
  8.     private int index;  
  9.     private Color(String name, int index) {  
  10.         this.name = name;  
  11.         this.index = index;  
  12.     }  
  13.     @Override  
  14.     public String getInfo() {  
  15.         return this.name;  
  16.     }  
  17.     @Override  
  18.     public void print() {  
  19.         System.out.println(this.index+":"+this.name);  
  20.     }  
  21. }  

 

 

 

常见枚举用法

标签:多继承   class   lang   覆盖   start   枚举   nbsp   相关   ace   

原文地址:https://www.cnblogs.com/yinghuoshouxin/p/9193356.html

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