On the definition and use of enumeration types in Java programming _java

Source: Internet
Author: User
Tags abstract define abstract

Defining an enumeration type is essentially defining a class, except that a lot of details are made up by the compiler, so the enum keyword acts like class or interface to some extent.

When you define an enumeration type using "Enum", essentially the type you define inherits from the Java.lang.Enum class, and the members of each enumeration are actually an instance of the enumerated type you define (Instance), and they are all defaulted, so you cannot change them. They are also static members, so you can use them directly through type names, and most importantly, they are public.

As an example:

Opconstants.java public
enum opconstants {turn_left, turn_right, SHOOT} 

In this example, Opconstants inherits from Java.lang.Enum, each enumeration member such as Turn_left, Turn_right, SHOOT, which is an object instance of the Opconstants, which is an object instance. Naturally, there are some methods that can be used, such as the ToString () method, which allows you to get a string description of the enumeration value directly, whereas the values () method of the enumeration object definition allows you to get all the enumerated instances and return them as an array. You use these two methods to simply display the contents of the opconstants:

Showenum.java public
class Showenum {public
  static void Main (string[] args) {for
    (Opconstants constant: Opconstants.values ()) {
      System.out.println (constant.tostring ());
    }
}} 

basically println () automatically calls ToString (), so it's OK to not write ToString (), and the results are as follows:

Turn_left
turn_right
SHOOT

You can use the "= =" or the Equals () method to compare enumerated objects, "" = = "compares the enumeration objects you provide to the same (that is, the same memory location), while Equals () is the content of the two enumerated objects, which are compared by default based on the enumerated string values.

The valueof () method allows you to convert the specified string to an enumeration instance, and you can use the CompareTo () method, which compares the order of the two enumerated objects at the time of enumeration, as shown in the following example

Showenum.java public
class Showenum {public
  static void Main (string[] args) {
    Enumcompareto ( Opconstants.valueof (Args[0]));
 
  public static void Enumcompareto (Opconstants constant) {
    System.out.println (constant);
    For (opconstants c:opconstants.values ()) {
      System.out.println (Constant.compareto (c));}}}
 

Execution results:

$java Showenum Turn_right
Turn_right
1
0
-1

Returns a positive value that indicates that the order is preceded by a negative representation of the enumerated object being compared, while 0 indicates that the position of the two reciprocal enumeration values is the same.

For each enumeration member, we can use the ordinal () method to get the location index in the order of enumeration, starting with 0 by default, for example:

Showenum.java public
class Showenum {public
  static void Main (string[] args) {for
    (opconstants c:opconstant S.values ()) {
      System.out.printf ("%d%s%n", c.ordinal (), c);}}
 

Execution results:

0 turn_left
1 turn_right
2 SHOOT

The enum keyword can be used to define an enumeration class, and you can write related constants in a class, and let's take a look at the example below.

The following code:

Class TestClass {private

  TestClass () {}//defines a private constructor and cannot instantiate objects from outside

  //provide two instances A, B public

  static final TestClass a=new TestClass ();

  public static final TestClass b=new TestClass ();

}

You can use an enumeration type instead:

Enum testclass01{

  a,b;

}

Usage:

An enumeration can also have constructors, fields, and methods:

Enum testclass02{

  A ("a")//The constructor parameters are passed in when the object is established

  , B ("B");

  private String value;

  Private TESTCLASS02 (String value) {

    This.value=value

  }

  Public String GetValue () {return

    this.value

  }}



Enumerations can also have abstract methods:

Enum testclass03{

  A () {

  //Implementation abstract method public

  void type () {

    System.out.println ("excellent");

  }

  , B () {public

  void type () {

    System.out.println ("benign");

  }

  ;

  public abstract void type ();//define abstract method

}

Test method:

public class Test02 {public

  static void Main (string[] args) {

    print (testclass02.a);/Incoming enumeration argument

  }

  public static void print (TestClass02 t) {

    System.out.println (T.getvalue ());

  }

Enum testclass02{

  A ("a")

  , B ("B");

  private String value;

  Private TESTCLASS02 (String value) {

    This.value=value

  }

  Public String GetValue () {return This.value}}}



Run Result:

A

Author: The beginning
Sign: As long as you are still trying, it is not a failure.

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.