当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Java的Enum的使用与分析

详解Java的Enum的使用与分析

2019年07月22日  | 移动技术网IT编程  | 我要评论

盲点 美国电影,查找qq号,1288dy影视

java  enum 使用

示例:

public enum enumtest {
   frank("the given name of me"),
   liu("the family name of me");
   private string context;
   private string getcontext(){
  
 return this.context;
   }
   private enumtest(string context){
  
 this.context = context;
   }
   public static void main(string[] args){
  
 for(enumtest name :enumtest.values()){
  
 system.out.println(name+" : "+name.getcontext());
  
 }
  
 system.out.println(enumtest.frank.getdeclaringclass());
   }
} 

 java中枚举实现的分析:

示例:

public enum color{ 
  red,blue,black,yellow,green 
} 

 显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类。 而这些类都是类库中enum类的子类(java.lang.enum<e>)。它们继承了这个enum中的许多有用的方法。我们对代码编译之后发现,编译器将enum类型单独编译成了一个字节码文件:color.class。

color字节码代码

final enum hr.test.color { 
  
 // 所有的枚举值都是类静态常量 
 public static final enum hr.test.color red; 
 public static final enum hr.test.color blue; 
 public static final enum hr.test.color black; 
 public static final enum hr.test.color yellow; 
 public static final enum hr.test.color green; 
  
private static final synthetic hr.test.color[] enum$values; 
  
 // 初始化过程,对枚举类的所有枚举值对象进行第一次初始化 
 static { 
    0 new hr.test.color [1]  
   3 dup 
   4 ldc <string "red"> [16] //把枚举值字符串"red"压入操作数栈 
   6 iconst_0 // 把整型值0压入操作数栈 
   7 invokespecial hr.test.color(java.lang.string, int) [17] //调用color类的私有构造器创建color对象red 
   10 putstatic hr.test.color.red : hr.test.color [21] //将枚举对象赋给color的静态常量red。 
   ......... 枚举对象blue等与上同 
  102 return 
}; 
  
 // 私有构造器,外部不可能动态创建一个枚举类对象(也就是不可能动态创建一个枚举值)。 
 private color(java.lang.string arg0, int arg1){ 
   // 调用父类enum的受保护构造器创建一个枚举对象 
   3 invokespecial java.lang.enum(java.lang.string, int) [38] 
}; 
  
 public static hr.test.color[] values(); 
  
  // 实现enum类的抽象方法  
 public static hr.test.color valueof(java.lang.string arg0); 
} 
 

下面我们就详细介绍enum定义的枚举类的特征及其用法。(后面均用color举例)

1、color枚举类就是class,而且是一个不可以被继承的final类。其枚举值(red,blue...)都是color类型的类静态常量, 我们可

以通过下面的方式来得到color枚举类的一个实例:

        color c=color.red;

注意:这些枚举值都是public static final的,也就是我们经常所定义的常量方式,因此枚举类中的枚举值最好全部大写。

2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同: 

      (1) 构造器只是在构造枚举值的时候被调用。

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; 
        } 
 
        public string tostring(){ //覆盖了父类enum的tostring() 
        return super.tostring()+"("+redvalue+","+greenvalue+","+bluevalue+")"; 
        } 
   
        private int redvalue; //自定义数据域,private为了封装。 
        private int greenvalue; 
        private int bluevalue; 
 } 

      (2) 构造器只能私有private,绝对不允许有public构造器。 这样可以保证外部代码无法新构造枚举类的实例。这也是完全符合情理的,因为我们知道枚举值是public static final的常量而已。 但枚举类的方法和数据域可以允许外部访问。

public static void main(string args[]) 
{ 
    // color colors=new color(100,200,300); //wrong 
      color color=color.red; 
      system.out.println(color); // 调用了tostring()方法 
}   

3、所有枚举类都继承了enum的方法,下面我们详细介绍这些方法。 

       (1)  ordinal()方法: 返回枚举值在枚举类种的顺序。这个顺序根据枚举值声明的顺序而定。

       color.red.ordinal(); //返回结果:0
         color.blue.ordinal(); //返回结果:1

       (2)  compareto()方法: enum实现了java.lang.comparable接口,因此可以比较象与指定对象的顺序。enum中的compareto返回的是两个枚举值的顺序之差。当然,前提是两个枚举值必须属于同一个枚举类,否则会抛出classcastexception()异常。(具体可见源代码)              

  color.red.compareto(color.blue); //返回结果 -1

       (3)  values()方法: 静态方法,返回一个包含全部枚举值的数组。             

  color[] colors=color.values();
         for(color c:colors){
            system.out.print(c+","); 
         }//返回结果:red,blue,black yellow,green,

       (4)  tostring()方法: 返回枚举常量的名称。              

  color c=color.red;
     system.out.println(c);//返回结果: red

       (5)  valueof()方法: 这个方法和tostring方法是相对应的,返回带指定名称的指定枚举类型的枚举常量。     

      color.valueof("blue");  //返回结果: color.blue

       (6)  equals()方法: 比较两个枚举类对象的引用。

//jdk源代码:   
public final boolean equals(object other) { 
    return this==other; 
}        

4、枚举类可以在switch语句中使用。

color color=color.red; 
switch(color){ 
    case red: system.out.println("it's red");break; 
    case blue: system.out.println("it's blue");break; 
    case black: system.out.println("it's blue");break; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网