All enumeration classes inherit from enum. Because Java does not support multiple inheritance, enumeration cannot inherit other classes.
However, enumeration can implement interfaces like normal classes.
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {color.setColor();}}interface Paint {public void setColor();}public enum Color implements Paint {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}// @Override// public String toString() {// return this._name.toUpperCase();// }@Overridepublic void setColor() {System.out.println("Current paint color: " + _name);}}}
The custom interface paint, which is used to enumerate the color.
If you select color for paint, can you specify the paint type?
Then, we transform the paint interface:
interface Paint {public void setColor();public void setType();}
Enumeration color for painting:
public enum Color implements Paint {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}// @Override// public String toString() {// return this._name.toUpperCase();// }@Overridepublic void setColor() {System.out.println("Current paint color: " + _name);}@Overridepublic void setType() {System.out.println("Current paint type: " + "How to determine??");}}
This problem is embodied in:
When selecting red, I want to select a paint of the type 5
When selecting Green, I want to select a paint of Type 1. What should I do?
....
(This is just to describe the enumeration function, not to better find a solution to the above problem. Do not run the question !)
Java enumeration in the previous blog: to understand the nature of enumeration,
The constant value of enumeration is essentially an enumeration object, so we can use an anonymous internal class to solve this problem!
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {color.setColor();color.setType();}}interface Paint {public void setColor();public void setType();}public enum Color implements Paint {RED("red color", 0) {@Overridepublic void setColor() {System.out.println("Current paint color: " + RED._name);}@Overridepublic void setType() {System.out.println("Current paint type: " + "5");}},GREEN("green color", 1) {@Overridepublic void setColor() {System.out.println("Current paint color: " + GREEN._name);}@Overridepublic void setType() {System.out.println("Current paint type: " + "1");}},BLUE("blue color", 2) {@Overridepublic void setColor() {System.out.println("Current paint color: " + BLUE._name);}@Overridepublic void setType() {System.out.println("Current paint type: " + "12");}},YELLOW("yellow color", 3) {@Overridepublic void setColor() {System.out.println("Current paint color: " + YELLOW._name);}@Overridepublic void setType() {System.out.println("Current paint type: " + "80");}};Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}}}
To present different paints, enumeration allows each of the enumerated values to implement the interface.
I recommend a blog: Discussing Java Enum Polymorphism