Package com.test.enums;
Import Java.util.EnumMap;
Import Java.util.EnumSet;
/**
*
* @ClassName: Testenum
*
* @Description: Test Enumeration
* @author Pinetree
* @date June 4, 2015 9:54:34
*
*/
public class Testenum {
public static void Main (string[] args) {
New Testenum (). change (); Test Normal enumeration
New Testenum (). changes (); Testing Custom Enumerations
New Testenum (). Overridemethod (); Override method in Test enumeration
New Testenum (). Interfacecolor (); Testing enumerations implementing external interface classes
1. Traversing enumeration types
System. Out. println ("... Demonstrate enumeration type traversal ... ");
Testtraversalenum ();
2. Demonstrating the use of Enummap objects
System. Out. println ("... Demonstrate the use and traversal of Enmumap objects ... ");
Testenummap ();
3. Demonstrating the use of Enmuset
System. Out. println ("... Demonstrate the use and traversal of Enmuset objects ... ");
Testenumset ();
}
/**
* Normal Enumeration
*/
public enum Color {
RED, GREEN, BLANK, YELLOW
}
/**
* Add a new method to the enumeration
*/
public enum Colors {
Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
Member variables
private String name;
private int index;
Construction method
Private Colors (String name, int index) {
THIS.name = name;
This.index = index;
}
Common methods
public static String getName (int index) {
For (Colors c:colors.values ())
if (c.getindex () = = index)
return c.name;
return null;
}
Get and Set methods
public void SetName (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
public int GetIndex () {
return index;
}
}
public enum Overridemethod {
Red (' Red ', ' red '), Green ("green", ' green '), BLANK ("white", ' white '), Yello ("Yellow", ' yellow ');
private String color; Color
private char C; Monochrome
Private Overridemethod (String color, char c) {
This.color = color;
THIS.C = C;
}
Get, Set method
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
Public Char GetC () {
return C;
}
public void SetC (char c) {
THIS.C = C;
}
@Override
Public String toString () {
Return This.color + "_" + C;
}
}
/**
* Implement external interface
*/
Public enum Interfacecolor implements behaviour{
Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
Member variables
private String name;
private int index;
Construction method
Private Interfacecolor (String name, int index) {
THIS.name = name;
This.index = index;
}
Implementing an interface method
@Override
public void print () {
System.out.println (This.index + "\ T" + this.name);
}
@Override
Public String GetInfo () {
return name;
}
}
Public enum Light {
RED (1), GREEN (2), Yello (3);
private int nCode;
Private light (int nCode) {
This.ncode = NCode;
}
@Override
Public String toString () {
Return string.valueof (NCode);
}
}
/**
* Test Normal enumeration
*/
public void Change () {
Color C = color.green;
Switch (c) {
Case RED:
System.out.println ("RED");
Break
Case GREEN:
System.out.println ("GREEN");
Break
Case BLANK:
System.out.println ("BLANK");
Break
Case YELLOW:
System.out.println ("YELLOW");
Break
Default
Break
}
}
/**
* Custom method in Test enumeration
*/
public void changes () {
try {
System.out.println (Colors.BLANK.getName () + "\ T" + Colors.BLANK.getIndex () + "\ n"
+ Colors.BLANK.name + "\ T" + Colors.BLANK.index + "\ T" + colors.getname (6));
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Inheritance Enumeration Internal methods
*/
public void Overridemethod () {
try {
Overridemethod om = Overridemethod.yello;
System.out.println (OverrideMethod.GREEN.toString () + "\ n"
+ om.getc () + "_" + Om.getcolor ());
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Implement external interface
*/
public void Interfacecolor () {
try {
Interfacecolor ic = interfacecolor.red;
Ic.print ();
System.out.println (Ic.getinfo ());
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Demonstration of enumeration type traversal
*/
public static void Testtraversalenum () {
light[] Alllight = Light.values ();
for (light light:alllight)
SYSTEM.OUT.PRINTLN ("Current lamp name:" + light.name () + "\ t current light ordinal:" + light.ordinal () + "\ t current lamp:" + light);
}
/**
* Demonstrates the use of Enummap, Enummap is similar to HashMap, except that if key is enumerated type
*/
public static void Testenummap () {
1. Demonstrates defining a Enummap object, the constructor of the Enummap object requires a parameter to pass in, and the default is the type of the key's class
Enummap<light, string> currenummap = new enummap<> (light.class);
Currenummap.put (light.red, "red light");
Currenummap.put (Light.green, "green light");
Currenummap.put (Light.yello, "yellow");
2. Traversing objects
For (Light light:Light.values ())
System.out.println ("key =" + light.name () + "\ t value =" + Currenummap.get (light));
}
/**
* Demonstrates how enumset is used, Enumset is an abstract class that gets a type of enum type content <BR/> can use the AllOf method
*/
public static void Testenumset () {
enumset<light> Currenumset = Enumset.allof (Light.class);
for (light light:currenumset)
System.out.println (Light.name () + "Current Enumset data is" + light);
}
}
Enum Enum type uses