Java enumeration: Implementation Interface

Source: Internet
Author: User

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.