`
leonzhx
  • 浏览: 796607 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 19. Enumerated Types -- Thinking in Java

阅读更多

1) You can step through the list of enum constants by calling values( ) on the enum. The values( ) method produces an array of the enum constants in the order in which they were declared, so you can use the resulting array in (for example) a foreach loop.

 

2) When you create an enum, an associated class is produced for you by the compiler. This class is automatically inherited from java.lang.Enum.

 

3) The ordinal( ) method produces an int indicating the declaration order of each enum instance, starting from zero. You can always safely compare enum instances using ==, and equals( ) and hashCode( ) are automatically created for you. The Enum class is Comparable, so there’s a compareTo( ) method, and it is also Serializable. If you call getDeclaringClass( ) on an enum instance, you’ll find out the enclosing enum class. The name( ) method produces the name exactly as it is declared, and this is what you get with toString( ), as well. valueOf( ) is a static member of Enum, and produces the enum instance that corresponds to the String name you pass to it, or throws an exception if there’s no match.

 

4) Except for the fact that you can’t inherit from it, an enum can be treated much like a regular class. This means that you can add methods to an enum. It’s even possible for an enum to have a main( ).

 

5) Notice that if you are going to define methods you must end the sequence of enum instances with a semicolon. Also, Java forces you to define the instances as the first thing in the enum. The constructor can only be private or package access and the compiler won’t let you use it to create any new instances once the enum definition is complete. You must specify the constructor arguement in the brackets right after where you define the enum instance.

 

6) Although normally you must qualify an enum instance with its type, you do not have to do this in a case statement.

 

7) There is no values( ) method in the Enum's definition. values( ) is a static method that is added by the compiler. There is a getEnumConstants( ) method in Class, so even if values( ) is not part of the interface of Enum, you can still get the enum instances via the Class object.

 

8) We’ve established that all enums extend java.lang.Enum. Since Java does not support multiple inheritance, this means that you cannot create an enum via inheritance. However, it is possible to create an enum that implements one or more interfaces.

 

9) The EnumSet was added to Java SE5 to work in concert with enums to create a replacement for traditional int-based "bit flags." The EnumSet is designed for speed, because it must compete effectively with bit flags (operations will be typically much faster than a HashSet). Internally, it is represented by (if possible) a single long that is treated as a bit-vector, so it’s extremely fast and efficient.

 

10) An EnumMap is a specialized Map that requires that its keys be from a single enum. Because of the constraints on an enum, an EnumMap can be implemented internally as an array. Thus they are extremely fast, so you can freely use EnumMaps for enum-based lookups. You can only call put( ) for keys that are in your enum, but other than that it’s like using an ordinary Map. Just as with EnumSet, the order of elements in the EnumMap is determined by their order of definition in the enum.

 

11) Java enums have a very interesting feature that allows you to give each enum instance different behavior by creating methods for each one. To do this, you define one or more abstract methods as part of the enum, then define the methods for each enum instance. This is often called table-driven code. The syntax for defining a constant-specific method is effectively that of an anonymous inner class, but more succinct.

 

12) Each enum element is actually a static final instance of the enum type. Also, because they are static, enum instances of inner enums do not behave like ordinary inner classes; you cannot access non-static fields or methods in the outer class. ( In this case , inner enum is a static inner class.)

分享到:
评论

相关推荐

    SystemVerilog for Design(2nd edition)

    2.2.4 Coding guidelines for importing packages into $unit ......................................19 2.2.5 Synthesis guidelines .............................................................................

    JAVA认证考试模拟题

    The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable. **正确答案:** A. The expressions (ONE == ONE)...

    Data.Structures.and.Algorithms.Made.Easy.epub

    "Data Structures And Algorithms Made Easy: Data Structure And Algorithmic Puzzles" is a book that ...Chapter 19. Dynamic Programming Chapter 20. Complexity Classes Chapter 21. Miscellaneous Concepts

    flexmark-ext-enumerated-reference-0.42.8.jar

    java运行依赖jar包

    flexmark-ext-enumerated-reference-0.40.20.jar

    java运行依赖jar包

    Thinking in Java 4th 习题答案

    《Thinking in Java 4th 习题答案》涵盖了多个关键的Java编程概念和技术,包括深入的容器理解、输入/输出(I/O)处理、枚举类型(Enumerated Types)、注解(Annotations)以及并发编程(Concurrency)。这些章节是Java学习...

    HIbernate annotation使用手册

    Hibernate 是一款非常流行的 ORM(Object Relational Mapping)框架,它能够简化 Java 应用程序与数据库之间的交互。为了更好地使用 Hibernate 进行开发,开发者通常会利用 Hibernate 提供的注解功能来简化实体类的...

    Packt.Mastering.Csharp.and.NET.Programming

    - **Covariance in Generic Types**: Explains how covariance works with generic types. - **Other Advanced Topics**: - **Dynamic Programming Features**: Introduces dynamic typing in C# and how it ...

    delphi2-delphi2010 全支持 dcu 装换 pas

    delphi2-delphi2010 全支持 dcu 装换 pas -----------------------------------------------------------------... - Bugs in enumerated type fixed - Bugs in classify interface and implement declarations fixed

    Device Console (DevCon) Tool_01_源码修正编译通过,VS2013可调试.rar

    sethwid Modify Hardware ID's of listed root-enumerated devices. stack List expected driver stack for devices. status List running status of devices. update Update a device manually. updateni Manually ...

    hibernate-annotations-3.4.0.GA and hibernate-entitymanager-3.4.0.GA

    Hibernate作为Java领域中最流行的ORM(对象关系映射)框架之一,极大地简化了数据库操作。本篇将详细探讨`hibernate-annotations-3.4.0.GA`和`hibernate-entitymanager-3.4.0.GA`这两个版本中的核心知识点,尤其是...

    Java教程补充材料

    6 Enumerated Types 7 Packages 8 Regular Expressions 9 Formatted Strings 10 The Methods in the Object Class (finalize, hashcode, clone, getClass, equals) 11 Hiding Data Fields and Static Methods ...

    XML实用大全----xml详细参考书

    10.5.2 Enumerated属性类型... 290 10.5.3 NMTOKEN属性类型... 291 10.5.4 NMTOKENS属性类型... 291 10.5.5 ID属性类型... 292 10.5.6 IDREF属性类型... 292 10.5.7 ENTITY属性类型... 293 10.5.8 ENTITIES ...

    XML编程综合资料库

    10.5.2 Enumerated属性类型... 290 10.5.3 NMTOKEN属性类型... 291 10.5.4 NMTOKENS属性类型... 291 10.5.5 ID属性类型... 292 10.5.6 IDREF属性类型... 292 10.5.7 ENTITY属性类型... 293 10.5.8 ENTITIES ...

    The Transport Layer Security (TLS) Protocol V1.1

    - **Enumerateds**: Lists enumerated types used in the protocol. - **Constructed Types**: Explains the construction of complex data types. - **Variants**: Specifies different variants of data types. - ...

    XML实用大全(web开发必备手册)

    10.5.2 Enumerated属性类型... 290 10.5.3 NMTOKEN属性类型... 291 10.5.4 NMTOKENS属性类型... 291 10.5.5 ID属性类型... 292 10.5.6 IDREF属性类型... 292 10.5.7 ENTITY属性类型... 293 10.5.8 ENTITIES ...

    pfc函數介紹

    - `e`: Enumerated (枚举) - `i`: Integer (整型) - `l`: Long (长整型) - `r`: Real (实数) - `s`: String (字符串) - `tm`: Time (时间) - `ui`: Unsigned Integer (无符号整型) - `ul`: Unsigned Long (无符号长...

Global site tag (gtag.js) - Google Analytics