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

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

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

    Jafe Lee

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      49 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
    Java 5.0新引進了一種類型:枚舉類型。昨晚看了一下,語法還是比較復雜的,至少比C的枚舉要復雜的很多,不過功能也強大了很多。具體語法請參見 JLS 8.9
    1、定義一個功能簡單的枚舉類型,更定義一個簡單的類很相似,例如
    package ?basic;
    public ? enum ?Day
    {
    ????MONDAY,?TUESDAT,?WEDNESDAY,?THURSDAY,?FRIDAY,?SATURDAY,?SUNDAY
    }
    • 跟類定義一樣,枚舉類型可以單獨放在一個文件里,當一個枚舉類型用public修飾時,它對其他包可見,否則只對同一個包中的類可見,這和類定義是一樣的。
    • 標識符 MONDAY, TUESDAY等就稱為枚舉常量(enumeration constants)
    • 每一個枚舉常量被隱式的聲明成Day的一個public、static成員,而且其類型為Day,亦就是說這些常量是self-typed的
    2、下面的定義也是合法的:
    package?basic;

    public?enum?Day
    {
    ????MONDAY,?TUESDAT,?WEDNESDAY,?THURSDAY,?FRIDAY,?SATURDAY,?SUNDAY,
    }

    package?basic;

    public?enum?Day
    {
    ????MONDAY,?TUESDAT,?WEDNESDAY,?THURSDAY,?FRIDAY,?SATURDAY,?SUNDAY,;
    }

    package?basic;

    public?enum?Day
    {
    ????MONDAY,?TUESDAT,?WEDNESDAY,?THURSDAY,?FRIDAY,?SATURDAY,?SUNDAY;
    }
    但是當枚舉類型有其他定義時,則分號;是必須的
    3、聲明、使用一個枚舉類型:
    (1)在同一個包中:

    BasicMainClass.java

    (2)在不同包中:

    OtherMainClass.java

    4、枚舉類型的性質:(摘自o'relly 出版的 Java in A Nutshell 5th)

    • Enumerated types have no public constructor. The only instances of an enumerated type are those declared by the enum.

    • Enums are not Cloneable, so copies of the existing instances cannot be created.

    • Enums implement java.io.Serializable so they can be serialized, but the Java serialization mechanism handles them specially to ensure that no new instances are ever created.

    • Instances of an enumerated type are immutable: each enum value retains its identity. (We'll see later in this chapter that you can add your own fields and methods to an enumerated type, which means that you can create enumerated values that have mutable portions. This is not recommended, but does not affect the basic identity of each value.)

    • Instances of an enumerated type are stored in public static final fields of the type itself. Because these fields are final, they cannot be overwritten with inappropriate values: you can't assign the DownloadStatus.ERROR value to the DownloadStatus.DONE field, for example.

    • By convention, the values of enumerated types are written using all capital letters, just as other static final fields are.

    • Because there is a strictly limited set of distinct enumerated values, it is always safe to compare enum values using the = = operator instead of calling the equals() method.

    • Enumerated types do have a working equals( ) method, however. The method uses = =finalso that it cannot be overridden. This working equals( ) method allows enumerated values to be used as members of collections such as Set, List, and Map. internally and is

    • Enumerated types have a working hashCode() method consistent with their equals( )equals(), hashCode( ) is final. It allows enumerated values to be used with classes like java.util.HashMap. method. Like

    • Enumerated types implement java.lang.Comparable, and the compareTo() method orders enumerated values in the order in which they appear in the enum declaration.

    • Enumerated types include a working toString( ) method that returns the name of the enumerated value. For example, DownloadStatus.DONE.toString( ) returns the string "DONE" by default. This method is not final, and enum types can provide a custom implementation if they choose.

    • Enumerated types provide a static valueOf( ) method that does the opposite of the default
      toString( ) method. For example, DownloadStatus.valueOf("DONE") would return DownloadStatus.DONE.

    • Enumerated types define a final instance method namedordinal()that returns an integer for each enumerated value. The ordinal of an enumerated value represents its position (starting at zero) in the list of value names in the enum declaration. You do not typically need to use the ordinal( ) method, but it is used by a number of enum-related facilities, as described later in the chapter.

    • Each enumerated type defines a static method named values( ) that returns an array of enumerated values of that type. This array contains the complete set of values, in the order they were declared, and is useful for iterating through the complete set of possible values. Because arrays are mutable, the values( ) method always returns a newly created and initialized array.

    • Enumerated types are subclasses of java.lang.Enum, which is new in Java 5.0. (Enum is not itself an enumerated type.) You cannot produce an enumerated type by manually extending the Enum class, and it is a compilation error to attempt this. The only way to define an enumerated type is with the enum keyword.

    • It is not possible to extend an enumerated type. Enumerated types are effectively final, but the final keyword is neither required nor permitted in their declarations. Because enums are effectively final, they may not be abstract.

    • Like classes, enumerated types may implement one or more interfaces.?


    posted on 2007-09-08 14:13 Jafe Lee 閱讀(14733) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲a∨无码男人的天堂| 波多野结衣在线免费视频| 久久久久亚洲国产AV麻豆| 亚洲成人在线网站| 亚洲XX00视频| 日本一道在线日本一道高清不卡免费 | 豆国产96在线|亚洲| 亚洲性一级理论片在线观看| 国产日产亚洲系列| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 国产在线观看xxxx免费| 国产亚洲综合久久| 亚洲午夜一区二区三区| 亚洲伊人久久大香线蕉| 色婷婷亚洲十月十月色天| 亚洲AV综合色区无码一区爱AV| 国产成人亚洲影院在线观看| av无码东京热亚洲男人的天堂| 夜夜嘿视频免费看| 成人免费毛片内射美女APP| 1000部拍拍拍18勿入免费凤凰福利| 成人免费区一区二区三区| 两性色午夜视频免费网| 亚洲免费一区二区| 国产精品福利在线观看免费不卡 | 超pen个人视频国产免费观看 | 亚洲欧美日韩中文无线码 | 一区二区三区在线观看免费 | 亚洲一区精品无码| 国产亚洲精品不卡在线| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 久久精品国产亚洲综合色| 精品亚洲综合在线第一区| 亚洲男人的天堂www| 国产AV无码专区亚洲AV毛网站| 亚洲熟妇无码乱子AV电影| 亚洲av无码不卡一区二区三区| 亚洲成A∨人片在线观看不卡| 久久亚洲一区二区| 97久久精品亚洲中文字幕无码| 亚洲精品在线视频观看|