Java中enum和C#中的異同

來源:互聯網
上載者:User

枚舉類型是JDK5.0的新特徵。Sun引進了一個全新的關鍵字enum來定義一個枚舉類。下面就是一個典型枚舉類型的定義:

Java代碼 public enum Color{            RED,BLUE,BLACK,YELLOW,GREEN        }  

顯然,enum很像特殊的class,實際上enum聲明定義的類型就是一個類。而這些類都是類庫中Enum類的子類(java.lang.Enum<E>)。它們繼承了這個Enum中的許多有用的方法。下面我們就詳細介紹enum定義的枚舉類的特徵及其用法。(後面均用Color舉例)

1、Color枚舉類是特殊的class,其枚舉值(RED,BLUE...)是Color的類對象(類執行個體):
                     Color c=Color.RED;
    而且這些枚舉值都是public static final的,也就是我們經常所定義的常量方式,因此枚舉類中的枚舉值最好全部大寫。

2、即然枚舉類是class,當然在枚舉類型中有構造器,方法和資料域。但是,枚舉類的構造器有很大的不同:
      (1) 構造器只是在構造枚舉值的時候被調用。

Java代碼 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(){  //自訂的public方法                    return super.toString()+"("+redValue+","+greenValue+","+blueValue+")";                        }                                   private int redValue;  //自訂資料域,private為了封裝。                        private int greenValue;                        private int blueValue;         }  

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,intbv){

                 this.redValue=rv;

                 this.greenValue=gv;

                 this.blueValue=bv;

                }

 

                public String toString(){  //自訂的public方法

                returnsuper.toString()+"("+redValue+","+greenValue+","+blueValue+")";

                }

  

                private int redValue;  //自訂資料域,private為了封裝。

                private int greenValue;

                private int blueValue;

 }

      (2) 構造器只能私人private,絕對不允許有public構造器。這樣可以保證外部代碼無法新構造枚舉類的執行個體。這也是完全符合情理的,因為我們知道枚舉值是public static final的常量而已。 但枚舉類的方法和資料域可以允許外部存取。

Java代碼 public static void main(String args[])        {                // Color colors=new Color(100,200,300);  //wrong                   Color color=Color.RED;                   System.out.println(color);  // 調用了toString()方法    }     

public static void main(Stringargs[])

{

        // Color colors=newColor(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()方法: 比較兩個枚舉類對象的引用。

Java代碼 //JDK原始碼:        public final boolean equals(Object other) {                return this==other;        }               

//JDK原始碼:   

public final booleanequals(Object other) {

        return this==other;

}           


4、枚舉類可以在switch語句中使用。

Java代碼 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;        }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.