For more information, see J2SE 5.0 Enumeration type.

Source: Internet
Author: User

 

So, cheer !!! J2SE 5.0 beta has been released, including the implementation of JSR 201 (currently in the public review State). JSR 201 contains the new language feature: enum. I think I should try it. The following is the result. If you want to give it a try, come on.

 

In-depth

An example of defining and using enumeration types is listed here (from the JSR 201 document ).

Java code

Public class Card

{

Public enum Suit {HEART, DIAMOND, CLUB, SPADE };

Public enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,

EIGHT, NINE, TEN, JACK, QUEEN, KING };

 

Private final Suit suit;

Private final Rank rank;

 

Public Card (Suit suit, Rank rank );{

This. suit = suit;

This. rank = rank;

}

 

Public Suit getSuit ();{

Return suit;

}

Public Rank getRank ();{

Return rank;

}

Public String toString ();{

Return rank + "of" + suit;

}

 

Public static void main (String... args );{

Card upMySleeve = new Card (Suit. HEART, Rank. ACE );;

System. out. println ("what is up my sleeve? "+ UpMySleeve );;

// Outputs: what is up my sleeve? ACE of HEART

}

}

Java code

Public class Card

{

Public enum Suit {HEART, DIAMOND, CLUB, SPADE };

Public enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,

EIGHT, NINE, TEN, JACK, QUEEN, KING };

 

Private final Suit suit;

Private final Rank rank;

 

Public Card (Suit suit, Rank rank );{

This. suit = suit;

This. rank = rank;

}

 

Public Suit getSuit ();{

Return suit;

}

Public Rank getRank ();{

Return rank;

}

Public String toString ();{

Return rank + "of" + suit;

}

 

Public static void main (String... args );{

Card upMySleeve = new Card (Suit. HEART, Rank. ACE );;

System. out. println ("what is up my sleeve? "+ UpMySleeve );;

// Outputs: what is up my sleeve? ACE of HEART

}

}

 

In some languages, the enumeration type is only an int in sheepskin. However, in the above example, Suit is a real class. Actually, it is a subclass of java. lang. Enum.

 

The code of the enumerated Suit type after compilation may look like this. In fact, every constant of the enumerated Suit type is an instance of Suit declared as public static final.

Java code

// This is sort of what happens.

Public class Suit extends Enum {

Public static final Suit HEART = new Suit ("HEART", 1 );;

Public static final Suit DIAMOND = new Suit ("DIAMOND", 2 );;

Public static final Suit CLUB = new Suit ("CLUB", 3 );;

Public static final Suit SPADE = new Suit ("SPADE", 4 );;

 

Public Suit (String name, int ordinal); {super (name, ordinal );;}

 

// Some more stuff here

}

Java code

// This is sort of what happens.

Public class Suit extends Enum {

Public static final Suit HEART = new Suit ("HEART", 1 );;

Public static final Suit DIAMOND = new Suit ("DIAMOND", 2 );;

Public static final Suit CLUB = new Suit ("CLUB", 3 );;

Public static final Suit SPADE = new Suit ("SPADE", 4 );;

 

Public Suit (String name, int ordinal); {super (name, ordinal );;}

 

// Some more stuff here

}

 

But this is not all. If you take a closer look at java. lang. enum, you will have a big mouth, because you will see that Enum is actually declared as a generic class Enum <E extends Enum <E>.

My first reaction was, "What the fuck do you mean ?" The answer is below, but we need to warm up first.

 

 

What is the true meaning of Foo <T>?

Your brain neurons have established a connection with the "String-based List container" in the Java code List <String>. But what does Foo <String> mean?

Consider the following code

T is A type parameter (A), which means that the class body of Foo has been parameterized by A type (any type) T. 1. Type T (almost) can appear anywhere in the class body without worrying about what type T actually is.

2. The semantics of the type parameter depends on the person who wrote the class Foo. The Collection class uses the type parameter to indicate "this T set". Its semantics can be found in javadoc. For a more general Foo <T>, it is no longer a T container.

3. What we really want to say here is that type parameters allow users to use specific types without using cast when calling function methods.

 

So what does java. lang. Class <T> mean?

If you check JDK 1.5 javadoc, you will find that java. lang. class now has a type parameter ava. lang. Class <T>.

So, the Class is not a Collections API. What will your brain do? You can observe the static attribute Foo. class, which does not return class, but Class <Foo>.

The type parameter of the Class tells the user whether it represents the type, allowing the user to call some methods of the Class without the cast.

Taking newInstance () and cast () as examples, both return an object of the T type.

Java code

Class <Date> c1 = Date. class;

Class c2 = Date. class;

Date d1 = c1.newInstance (); // new usage, no ClassCastException

Date d2 = (Date); c2.newInstance (); // previous usage

 

Object o = d1;

 

// Manual cast is no longer required

// Throw ClassCastException when necessary

Date d3 = c1.cast (o );;

 

// Previous usage, manual cast required

Date d4 = (Date); o;

 

 

Class <Date> c1 = Date. class;

Class c2 = Date. class;

Date d1 = c1.newInstance (); // new style, no potential ClassCastException

Date d2 = (Date); c2.newInstance (); // old style

 

Object o = d1;

 

// No need to do a manual cast, the cast (); method

// Throws the ClassCastException if necessary

Date d3 = c1.cast (o );;

 

// Old style, need to do a manual cast

Date d4 = (Date); o;

 

Public class Foo <T> {// ();

//...

}

 

Foo <String> f1 =...; // (B );

Foo <Integer> f2 =...; // (C );

Java code

Class <Date> c1 = Date. class;

Class c2 = Date. class;

Date d1 = c1.newInstance (); // new usage, no ClassCastException

Date d2 = (Date); c2.newInstance (); // previous usage

 

Object o = d1;

 

// Manual cast is no longer required

// Throw ClassCastException when necessary

Date d3 = c1.cast (o );;

 

// Previous usage, manual cast required

Date d4 = (Date); o;

 

 

Class <Date> c1 = Date. class;

Class c2 = Date. class;

Date d1 = c1.newInstance (); // new style, no potential ClassCastException

Date d2 = (Date); c2.newInstance (); // old style

 

Object o = d1;

 

// No need to do a manual cast, the cast (); method

// Throws the ClassCastException if necessary

Date d3 = c1.cast (o );;

 

// Old style, need to do a manual cast

Date d4 = (Date); o;

 

Public class Foo <T> {// ();

//...

}

 

Foo <String> f1 =...; // (B );

Foo <Integer> f2 =...; // (C );

 

Therefore, the connection between brain neurons is established: Class <T> means Class instances of type T.

 

More tips on type parameters

Note the following code:

Java code

Abstract class Foo <SubClassOfFoo extends Foo <SubClassOfFoo>

{

/** Subclasses are forced to return themselves from this method */

Public abstract SubClassOfFoo subclassAwareDeepCopy ();;

}

 

Class Bar extends Foo <Bar> {

Public Bar subclassAwareDeepCopy ();{

Bar B = new Bar ();;

//...

Return B;

}

}

 

Bar B = new Bar ();;

Foo <Bar> f = B;

Bar b2 = B. subclassAwareDeepCopy ();;

Bar b3 = f. subclassAwareDeepCopy (); // This is the key. The parent class directly returns the subclass instance without the use of Cast.

Java code

Abstract class Foo <SubClassOfFoo extends Foo <SubClassOfFoo>

{

/** Subclasses are forced to return themselves from this method */

Public abstract SubClassOfFoo subclassAwareDeepCopy ();;

}

 

Class Bar extends Foo <Bar> {

Public Bar subclassAwareDeepCopy ();{

Bar B = new Bar ();;

//...

Return B;

}

}

 

Bar B = new Bar ();;

Foo <Bar> f = B;

Bar b2 = B. subclassAwareDeepCopy ();;

Bar b3 = f. subclassAwareDeepCopy (); // This is the key. The parent class directly returns the subclass instance without the use of Cast.

 

Here:

 

1. All subclass of Foo must provide a type parameter to Foo.

2. This type parameter must be a subclass of Foo.

3. The subclass of Foo transmits itself as a type parameter to Foo.

4 Foo has a method to return SubClassOfFoo. According to the above conditions, we can see that subclassAwareDeepCopy () must be implemented for any subclass of Foo. This method returns the subclass itself.

 

That is to say, through this method, the parent class (such as Abstract Factory) can directly use the subclass type (rather than the parent class type) as a function parameter or return value.

 

 

(Note: Condition 3 is not mandatory, but required. For example, in the preceding example, we can add another subclass of Foo.

Java code

Class Bar1 extends Foo <Bar> {

Public Bar subclassAwareDeepCopy ();{

Return new Bar ();;

}

}

Java code

Class Bar1 extends Foo <Bar> {

Public Bar subclassAwareDeepCopy ();{

Return new Bar ();;

}

}

 

Here, Bar1 does not have the ability to return an instance through subclassAwareDeepCopy)

 

Now let's look at the Enum <E extends Enum <E>

 

If you understand the rules used by the above Foo and its sub-classes, let's look at why java. lang. Enum is defined as Enum <E extends Enum <E>.

 

E is used as the return value of getDeclaringClass ().

E is used as the compareTo () parameter.

 

This means that you can complete the following code and get the following convenience. a) You do not need to force type conversion. B) Use the subclass of Enum as the parameter of the Enum method.

Java code

Rank r = Rank. ACE;

Suit s = Suit. HEART;

 

R. compareTo (s); // syntax error. The parameter must be of the Rank type.

 

Rank z = Enum. valueOf (Rank. class, "TWO"); // type conversion is not required

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.