Usage Five: Implement Interface
All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.
Java code
- Public interface Behaviour {
- void print ();
- String GetInfo ();
- }
- Public enum Color 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 Color (String name, int index) {
- this.name = name;
- This.index = index;
- }
- Interface method
- @Override
- Public String GetInfo () {
- return this.name;
- }
- //interface method
- @Override
- public void print () {
- System.out.println (this.index+":" +this.name);
- }
- }
Usage VI: Organizing enumerations using interfaces
Java code
- Public interface Food {
- enum Coffee implements food{
- Black_coffee,decaf_coffee,latte,cappuccino
- }
- enum Dessert implements food{
- FRUIT, CAKE, GELATO
- }
- }
Usage Seven: About the use of enumeration collections
Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.
The Java enum (enum) uses a detailed four