http://blog.csdn.net/veryitman/article/details/7945949
所有的枚举类都继承自 Enum,因为 Java 不支持多继承,所以枚举无法继承其它类。
但是,枚举与普通类一样,可以实现接口。
- 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();
- // }
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + _name);
- }
- }
- }
自定义接口 Paint,枚举 Color 实现该接口。
试想一下,为 Paint 选择 color 的同时是否可以指定 Paint 的类型呢?
那么,我们改造这个 Paint 接口:
- interface Paint {
- public void setColor();
- public void setType();
- }
枚举 Color 实现 Paint:
- 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();
- // }
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + _name);
- }
- @Override
- public void setType() {
- System.out.println("Current paint type: " + "How to determine??");
- }
- }
把这个问题具体化:
在选择 RED 的同时,我想选择类型为 5 号的 paint
在选择 GREEN 的同时,我想选择类型为 1 号的 paint,如何处理?
....
(这里只是为了说明枚举的功能,不是为了更好的找到解决上述问题的方法,请勿跑题!)
在前面的博客 Java 枚举:理解枚举本质 中说到,
枚举的常量值本质就是枚举对象,那么我们可以采用匿名内部类来解决这个问题!
- 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) {
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + RED._name);
- }
- @Override
- public void setType() {
- System.out.println("Current paint type: " + "5");
- }
- },
- GREEN("green color", 1) {
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + GREEN._name);
- }
- @Override
- public void setType() {
- System.out.println("Current paint type: " + "1");
- }
- },
- BLUE("blue color", 2) {
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + BLUE._name);
- }
- @Override
- public void setType() {
- System.out.println("Current paint type: " + "12");
- }
- },
- YELLOW("yellow color", 3) {
- @Override
- public void setColor() {
- System.out.println("Current paint color: " + YELLOW._name);
- }
- @Override
- public 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;
- }
- }
- }
为了呈现不同的 Paint,枚举可以让其每个枚举值实现接口。
推荐一篇博客:小谈Java Enum的多态性