<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-6  評(píng)論-38  文章-40  trackbacks-0
    Enum是enumeration(列舉)的簡寫形式,包含在java.lang包中.熟悉C, C++, C#, 或 Pascal人應(yīng)該對(duì)列舉有所了解,先看個(gè)例子:

    public enum Season { winter, spring, summer, fall }

    ?

    一個(gè)enum是定義一組值的對(duì)象,它可以包括零個(gè)或多個(gè)值成員.它是屬于enum類型的,一個(gè)enum對(duì)象中不可有兩個(gè)或多個(gè)相同的屬性或值.在次之前的java程序員一般是 用接口的方法實(shí)現(xiàn)列舉的,如 :

    public interface Season {

    ?? static winter = 0;

    ?? static spring = 1; //etc..

    }

    引入了enum的java的列舉的編寫方便了許多,只須定義一個(gè)enum型的對(duì)象.enum對(duì)象的值都回自動(dòng)獲得一個(gè)數(shù)字值,從0開始,依次遞增.看一個(gè)比較簡單的enum實(shí)現(xiàn)的例子:

    EnumDemo.java

    package net.javagarage.enums;

    ?

    /*

    We can loop over the values we put into the enum

    using the values() method.

    Note that the enum Seasons is compiled into a

    separate unit, called EnumDemo$Seasons.class

    */

    public class EnumDemo {

    ?

    ????? /*declare the enum and add values to it. note that, like in C#, we don't use a ; to

    ?end this statement and we use commas to separate the values */

    ?

    ????? private enum Seasons { winter, spring,

    ?????? summer, fall }

    ?

    ????? //list the values

    ????? public static void main(String[] args) {

    ??????????? for (Seasons s : Seasons.values()){

    ????????????????? System.out.println(s);

    ??????????? }

    ????? }

    ?

    }
    運(yùn)行上述代碼你回得到 以下結(jié)果:

    winter

    spring

    summer

    fall

    Enum的屬性調(diào)用:

    下面的代碼展示了調(diào)用enum對(duì)象的方法,這也是它通常的用法:

    package net.javagarage.enums;

    /*

    File: EnumSwitch.java

    Purpose: show how to switch against the values in an enum.

    */

    ?

    public class EnumSwitch {

    ?

    ????? private enum Color { red, blue, green }

    ?

    ????? //list the values

    ????? public static void main(String[] args) {

    ??????????? //refer to the qualified value

    ??????????? doIt(Color.red);

    ?

    ????? }

    ?

    ????? /*note that you switch against the UNQUALIFIED name. that is, "case Color.red:" is a

    ?compiler error */

    ?

    ????? private static void doIt(Color c){

    ?

    ????? switch (c) {

    ????? case red:

    ??????????? System.out.println("value is " + Color.red);

    ??????????? break;

    ????? case green:

    ??????????? System.out.println("value is " + Color.green);

    ??????????? break;

    ????? case blue:

    ??????????? System.out.println("value is : " + Color.blue);

    ??????????? break;

    ????? default :

    ??????????? System.out.println("default");

    ????? }

    ????? }
    }

    為enums添加屬性和方法

    enums也可以象一般的類一樣添加方法和屬性,你可以為它添加靜態(tài)和非靜態(tài)的屬性或方法,這一切都象你在一般的類中做的那樣.

    package net.javagarage.enums;

    ?

    /*

    File: EnumDemo.java

    Purpose: show how to use an enum that also defines its own fields and methods

    */

    ?

    public class EnumWithMethods {

    ?

    //declare the enum and add values to it.

    ?

    public enum Season {

    ?

    ????? winter, spring, summer, fall;

    ?

    ????? private final static String location = "Phoenix";

    ?

    ????? public static Season getBest(){

    ??????????? if (location.equals("Phoenix"))

    ????????????????? return winter;

    ??????????? else

    ????????????????? return summer;

    ?

    ????? }

    ?

    ????? public static void main(String[] args) {

    ?

    ????? System.out.println(Season.getBest());

    ????? }

    }

    就是這么的簡單.但是有一點(diǎn)是需要注意的,那就是enums的值列表必須緊跟在enum聲明,不然編譯時(shí)將會(huì)出錯(cuò).

    Enums構(gòu)造函數(shù):

    和類一樣enums也可以有自己的構(gòu)造函數(shù),如下:

    package net.javagarage.enums;

    ?

    public class EnumConstructor {

    ?

    ????? public static void main(String[] a) {

    ?

    ??????????? //call our enum using the values method

    ??????????? for (Temp t : Temp.values())

    ????????????????? System.out.println(t + " is : " + t.getValue());

    ????? }

    ?

    ????? //make the enum

    ????? public enum Temp {

    ??????????? absoluteZero(-459), freezing(32),

    ??????????? boiling(212), paperBurns(451);

    ?

    ????? //constructor here

    ????? Temp(int value) {

    ??????????? this.value = value;

    ????? }

    ?

    ????? //regular field?but make it final,

    ????? //since that is the point, to make constants

    ????? private final int value;

    ?

    ????? //regular get method

    ????? public int getValue() {

    ????? return value;

    ????? }

    ?

    ????? }

    }
    輸出結(jié)果是:

    absoluteZero is : -459

    freezing is : 32

    boiling is : 212

    paperBurns is : 451

    盡管enums有這么多的屬性,但并不是用的越多越好,如果那樣還不如直接用類來的直接.enums的優(yōu)勢(shì)在定義int最終變量僅當(dāng)這些值有一定特殊含義時(shí).但是如果你需要的是一個(gè)類,就定義一個(gè)類,而不是enum.

    posted on 2006-11-26 16:18 一手的小窩窩 閱讀(288) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 亚洲色成人WWW永久网站| 99国产精品免费视频观看| 成年女人视频网站免费m | 青青青免费国产在线视频小草| 国产亚洲精品一品区99热| 免费无码AV一区二区| 亚洲精品一级无码中文字幕| 一级做a爰性色毛片免费| 亚洲午夜国产片在线观看| 一级做a爱片特黄在线观看免费看| 亚洲欧洲日产国码一级毛片 | 精品亚洲成a人在线观看| 青青草国产免费久久久下载| 亚洲精品无码久久久久A片苍井空| 在线精品免费视频| 国产精品亚洲精品爽爽| 亚洲午夜国产片在线观看| 国产va在线观看免费| 亚洲人妖女同在线播放| 免费鲁丝片一级在线观看| 曰批全过程免费视频观看免费软件| 亚洲另类少妇17p| 国产日韩一区二区三免费高清| 色婷婷亚洲十月十月色天| 桃子视频在线观看高清免费完整| 亚洲欧美一区二区三区日产| 一区国严二区亚洲三区| 97性无码区免费| 亚洲人成色99999在线观看| 四虎亚洲国产成人久久精品 | 久久精品国产亚洲AV久| 国产激情久久久久影院老熟女免费| 亚洲人成色7777在线观看| 黄色网址免费观看| 国产亚洲精品美女2020久久| 国产AV无码专区亚洲AV男同| 一个人免费高清在线观看| 免费人人潮人人爽一区二区| 亚洲AV天天做在线观看| 日本不卡免费新一二三区| 中文字幕无码一区二区免费|