Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 214
نمايش فراداده

Enum<E extends Enum<E>>java.lang

Java 5.0serializable comparable

This class is the common superclass of all enumerated types. It is not itself an enum type, however, and a Java compiler does not allow other classes to extend it. Subclasses of Enum may be only created with enum declarations. Enum is a generic type, and the type variable E represents the concrete enumerated type that actually extends Enum. This type variable exists so that Enum can implement Comparable<E>.

Every enumerated constant has a name (the name it was declared with) and an ordinal valuethe first constant in an enum declaration has an ordinal of 0, the second has an ordinal of 1, and so on. The final methods name( ) and ordinal( ) return these values. Most users of enumerated constants will use toString( ) instead of name( ). The implementation of toString( ) defined by Enum returns the same value as name( ). The toString( ) method is not final, however, and it can be overridden in enum declarations.

Enum implements a number of Object and Comparable methods and makes its implementations final so that they are inherited by all enum types and may not be overridden. equals( ) compares enumerated constants with the = = operator, and hashCode( ) returns the System.identityHashCode( ) value. In order to make this identity-based equals( ) implementation work, Enum overrides the protected clone( ) method to throw CloneNotSupportedException, preventing additional copies of enumerated values from being created. Finally, the compareTo( ) method of the Comparable interface is defined to compare enumerated values based on their ordinal( ) value.

getdeclaringClass( ) returns the Class object that represents the enum type of which the constant is a part. It is like the getClass( ) method inherited from Object, but the return values of these two methods will be different for enumerated constants that have value-specific class bodies, since those constants are instances of an anonymous subclass of the enum type.

The static valueOf( ) method is passed the type and name of an enumerated constant and returns the object that represents that constant (or throws an IllegalArgumentException).

Figure 10-18. java.lang.Enum<E extends Enum<E>>

public abstract class

Enum<E extends Enum<E>> implements Comparable<E>, Serializable { // Protected Constructors protected

Enum (String

name , int

ordinal ); // Public Class Methods public static <T extends Enum<T>> T

valueOf (Class<T>

enumType , String

name ); // Public Instance Methods public final Class<E>

getDeclaringClass ( ); public final String

name ( ); public final int

ordinal ( ); // Methods Implementing Comparable public final int

compareTo (E

o ); // Public Methods Overriding Object public final boolean

equals (Object

other ); public final int

hashCode ( ); public String

toString ( ); // Protected Methods Overriding Object protected final Object

clone ( ) throws CloneNotSupportedException; }

Subclasses

THRead.State, java.lang.annotation.ElementType, java.lang.annotation.RetentionPolicy, java.lang.management.MemoryType, java.math.RoundingMode, java.net.Authenticator.RequestorType, java.net.Proxy.Type, java.security.KeyRep.Type, java.util.Formatter.BigDecimalLayoutForm, java.util.concurrent.TimeUnit, javax.net.ssl.SSLEngineResult.HandshakeStatus, javax.net.ssl.SSLEngineResult.Status

Passed To

Too many methods to list.