The Java enum (enum) uses a detailed four

Source: Internet
Author: User

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
  1. Public interface Behaviour {
  2. void print ();
  3. String GetInfo ();
  4. }
  5. Public enum Color implements behaviour{
  6. Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);
  7. //Member variables
  8. private String name;
  9. private int index;
  10. //Construction method
  11. Private Color (String name, int index) {
  12. this.name = name;
  13. This.index = index;
  14. }
  15. Interface method
  16. @Override
  17. Public String GetInfo () {
  18. return this.name;
  19. }
  20. //interface method
  21. @Override
  22. public void print () {
  23. System.out.println (this.index+":" +this.name);
  24. }
  25. }

Usage VI: Organizing enumerations using interfaces

Java code
    1. Public interface Food {
    2. enum Coffee implements food{
    3. Black_coffee,decaf_coffee,latte,cappuccino
    4. }
    5. enum Dessert implements food{
    6. FRUIT, CAKE, GELATO
    7. }
    8. }

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

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.