[Reserved] Java enumeration of use

Enumeration type can replace the conventional manner defined constants, i.e. constant in the class or interface package. In addition, the enumerated type also provides the security check. The enumerated type is essentially still exist as a class.

1, setting the enum constant
conventionally set constant, the constant typically placed in the interface, so that the program can be used directly, and the constant and can not be modified, because the constants defined in the interface, the modified constant symbols as final and static.

public interface Constants
{
public static final int RED = 1;
public static final int BLUE = 2;
public static final int GREEN = 3;
}

Enumeration constants defined using the following syntax:
public enum ColorEnum
{
RED,
BLUE,
GREEN
}

 

 

Naming conventions:

final constants: uppercase letter designation, and the intermediate connecting an underscore.

Enumeration enum: uppercase name, and the intermediate connecting an underscore.

Example: enum type.

public static void doit(ColorEnum c)
{
switch (c)
{
case RED:
System.out.println("This is RED");
break;
case BLUE:
System.out.println("This is BLUE");
break;
case GREEN:
System.out.println("This is GREEN");
break;
default:
break;
}
}

public static void main(String[] args)
{
doit(ColorEnum.RED);
}

 


 

2, in-depth understanding of the enumerated type
enumeration type constants defined in the more traditional way, in addition to the detection of parameter type has advantages, but also has other advantages.

2.1 The method of enumerated type operation member
user may be considered as an enumerated type is a class that inherits from class java.lang.Enum, when the definition of an enumerated type, each member of a enumerated type can be regarded as an example of an enumerated type, these enumerated types are modified by members of the default final, public, static, so when members directly using the enumerated type enumeration type name calling enumerated type member can be.

Since the enumeration type java.lang.Enum object inherits class, the class methods enumerated type operation can be applied to an enumeration type.

Common method of enumeration type:

Method Name Method specific sense
values () method may return enumerated type member .values enumerated type as an array name ()
valueOf () This method may be implemented to convert ordinary string type name enumerate instances enumerated .valueOf ( "the ABC")
the compareTo () this method is used to compare two objects in the order of enumeration defines enumerable object .compareTo ()
ORDINAL () this method is used to obtain location index enumeration object enumeration member. ORDINAL ()
(. 1) values () method

The method can be enumerated type returns an array of members, members can also get enumerated type by this method.

Example: Use of an enumerated type values ​​() method to get the member variables of the enumeration type.

/ ** 
* Use enumerated type values () method of obtaining a member of the enumerated type variables 
* 
* @author pan_junbiao 
* 
* / 
public  class ShowEnum 
{ 
enum ColorEnum 
{ 
RED, BLUE, GREEN 
} 

// cycle by the values () the method returns an array 
public  static  void main (String [] args) 
{ 
System.out.println ( "mode 1:" );
 for ( int . I = 0; I <ColorEnum.values () length; I ++ ) 
{ 
// printing member variables enumerated 
System.out.println ( "enumeration type member variables:" + ColorEnum.values () [I]); 
}

System.out.println ( "two ways:" );
 for (C ColorEnum: ColorEnum.values ()) 
{ 
// enumerated printing member variables 
System.out.println ( "enumeration type member variables:" + C) ; 
} 
} 
}

 


Results of the:

 

 

(2) valueOf () and the compareTo () method

Enumeration type static methods valueOf () may be implemented to convert a string of ordinary enumerate instances, while the compareTo () method is used in order to compare two objects defined when enumerated.

Example: enumeration valueOf () using the compareTo () method.

/ ** 
Use * enumeration valueOf () and the compareTo () method 
* 
* @author pan_junbiao 
* 
* / 
public  class EnumMethodTest 
{ 
enum ColorEnum 
{ 
RED, BLUE, GREEN 
} 

// definition of an enumerated type comparison method, parameters of type enumerated type 
public  static  void Compare (ColorEnum C) 
{ 
// do the array element values () method returns the cyclic operation 
for ( int I = 0;. I <ColorEnum.values () length; I ++ ) 
{ 
// the comparison result Back 
System.out.println (c + "and" + ColorEnum.values () [i] + " comparison results:" +  c.compareTo (ColorEnum.values () [I]));
} 
} 

public static  void main (String [] args) 
{ 
// use valueOf () to convert the string to an enumeration instance 
ColorEnum C = ColorEnum.valueOf ( "BLUE" ); 
Compare (C); 
} 
}

 


Results of the:

 

 

Description: Before calling the compareTo () method returns a result, positive values ​​indicate method calls enumeration object position parameters of the process; 0 represents two same position are compared with each enumeration members; negative values ​​indicate the process parameters this method is called after the enumeration object position.

(3) ordinal () method

The method used to obtain the position of the index enumeration members.

Example: enumeration ORDINAL () method is used.

/ ** 
Use enumeration ORDINAL * () method 
* 
* @author pan_junbiao 
* 
* / 
public  class EnumOrdinalTest 
{ 
public  enum ColorEnum 
{ 
RED, BLUE, GREEN 
} 

public  static  void main (String [] args) 
{ 
for ( int I 0 =; I ++; I <ColorEnum.values () length. ) 
{ 
// Get enumerated type member position index in the circulation 
System.out.println (ColorEnum.values () [i] + " in an enumerated type position index value "+ ColorEnum.values () [I] .ordinal ()); 
} 
} 
}

 


Results of the:

 

 

Constructor 2.2 of enum type in
an enumerated type, you can add a constructor, but the provisions of this construction method must be modified for the private modifier.

示例:在枚举类型中,可以添加构造方法。

/**
* 在枚举类型中添加构造方法
* 
* @author pan_junbiao
*
*/
public class EnumIndexTest
{
enum ColorEnum
{
RED(1, "我是红色"), BLUE(2, "我是蓝色"), GREEN(3, "我是绿色");

private final int value;
private final String description;

private ColorEnum(int value, String description)
{
this.value = value;
this.description = description;
}

public int getValue()
{
return this.value;
}

public String getDescription()
{
return this.description;
}

public static ColorEnum valueOf(int value)
{
switch (value)
{
case 1:
return ColorEnum.RED;
case 2:
return ColorEnum.BLUE;
case 3:
return ColorEnum.GREEN;
default:
return null;
}
}
}

public static void main(String[] args)
{
for (ColorEnum c : ColorEnum.values())
{
System.out.println("枚举成员:" + c + " 值:" + c.getValue() + " 描述:" + c.getDescription());
}

System.out.println("值转换成枚举:" + ColorEnum.valueOf(2));
System.out.println("字符串转换成枚举:" + ColorEnum.valueOf("GREEN"));
}
}

 


执行结果:

 

 

2.3 枚举中实现接口
除了可以使用上述示例的方法定义getDescription()方法获取枚举类型成员变量是的描述之外,还可以将这个getDescription()方法放置在接口中,使枚举类型实现接口,然后使每个枚举类型实现接口中的方法。

示例:在项目中创建d接口和枚举类型的AnyEnum类,在枚举类型AnyEnum类中实现带方法的接口,使每个枚举类型成员实现该接口中的方法。

interface d
{
public int getValue();

public String getDescription();
}

/**
* 枚举中实现接口
* 
* @author pan_junbiao
*
*/
public enum AnyEnum implements d
{
RED
{
public int getValue()
{
return 1;
}

public String getDescription()
{
return "我是红色";
}
},

BLUE
{
public int getValue()
{
return 2;
}

public String getDescription()
{
return "我是蓝色";
}
},

GREEN
{
public int getValue()
{
return 3;
}

public String getDescription()
{
return "我是绿色";
}
};

public static void main(String[] args)
{
for (AnyEnum c : AnyEnum.values())
{
System.out.println("枚举成员:" + c + " 值:" + c.getValue() + " 描述:" + c.getDescription());
}
}
}

 


执行结果:

 

 

 

3、使用枚举类型的优势
枚举类型声明提供了一种用户友好的变量定义方法,枚举了某种数据类型所有可能出现的值。总结枚举类型,它具有以下特点:

(1)类型安全。

(2)紧凑有效的数据定义。

(3)可以和程序其他部分完美交互。

(4)运行效率高。
---------------------
作者:pan_junbiao
来源:CSDN
原文:https://blog.csdn.net/pan_junbiao/article/details/85257445
版权声明:本文为博主原创文章,转载请附上博文链接!

Guess you like

Origin www.cnblogs.com/appium/p/11115981.html