Enumeration Summary (rpm)

2011-10-08  来源:本站原创  分类:Industry  人气:66 

DK1.5 introduced a new type - enumeration. In Java, although it considered a "small" function, gave me to bring the development of a "large" convenience.

Use a: constant

In JDK1.5, we define the constants are: publicstaticfianl ..... Well now, with the enumeration constants can be related to an enumerated type in the group, and enumeration constants to provide more than the method.

Java code

public enum Color {
  RED, GREEN, BLANK, YELLOW
}

Use of two: switch

JDK1.6 before the switch statement only supports int, char, enum types, enumerations, make our code more readable.

Java code

enum Signal {
    GREEN, YELLOW, RED
}
public class TrafficLight {
    Signal color = Signal.RED;
    public void change() {
        switch (color) {
        case RED:
            color = Signal.GREEN;
            break;
        case YELLOW:
            color = Signal.RED;
            break;
        case GREEN:
            color = Signal.YELLOW;
            break;
        }
    }
}

Use three: add new methods to enumerate

If you plan to customize their own methods, then the sequence must be the last enum instances to add a semicolon. Requirements must be defined and the Java enum instance.

Java code

public enum Color {
    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;
    }
    //  Common method
    public static String getName(int index) {
        for (Color c : Color.values()) {
            if (c.getIndex() == index) {
                return c.name;
            }
        }
        return null;
    }
    // get set  Method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }
}

Use four: cover enum

Here is a toString () method to overwrite the example.

Java code

public enum Color {
    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;
    }
    // Overriding methods
    @Override
    public String toString() {
        return this.index+"_"+this.name;
    }
}

Use five: implements the interface

Enumeration of all classes inherit from java.lang.Enum. Since Java does not support multiple inheritance, so the object can not inherit the other enumerated 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);
        }
    }

Use six: use the interface organization enumerated

public interface Food {
        enum Coffee implements Food{
            BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
        }
        enum Dessert implements Food{
            FRUIT, CAKE, GELATO
        }
    }

Usage seven: the use of enumeration

java.util.EnumSet and java.util.EnumMap two enumeration sets. EnumSet ensure collection element does not repeat; EnumMap the key is the enum type and value can be any type. The use of the two sets do not go into here, can refer to the JDK documentation.

相关文章
  • Enumeration Summary (rpm) 2011-10-08

    DK1.5 introduced a new type - enumeration. In Java, although it considered a "small" function, gave me to bring the development of a "large" convenience. Use a: constant In JDK1.5, we define the constants are: publicstaticfianl ..... W

  • Java relative path / absolute path to the Summary (rpm) 2010-09-27

    Absolute path: absolute path is your home files or directories on the real path on your hard drive, (URL and the physical path) example: C: xyz est.txt represents the absolute path of the file test.txt. http://www.sun.com/index.htm also represents th

  • jquery Skills Summary (rpm) 2010-12-30

    jquery Skills Summary (transfer): I. Introduction 1.1 Overview and ajax thoughts with WEB2.0 the rapid development of Internet communication, has been found in some excellent Js framework, one of the more famous are Prototype, YUI, jQuery, mootools,

  • VSS 2005 client and server configuration summary (rpm) 2011-02-23

    VSS 2005 client and server configuration summary 2009-05-13 17:14:25 Tags: It took almost a day to thoroughly understand the configuration vss ~ ado, introduced the following simple configuration process: Installation VSS2005 (client and server side

  • mysql 5.0 stored procedure study summary (rpm) 2010-09-12

    I. Creating a stored procedure 1 Basic syntax: create procedure sp_name () begin ... ... ... end 2 parameters II. Call a stored procedure 1 Basic syntax: call sp_name () Note: The stored procedure name must be followed by brackets, even if the stored

  • Chinese garbled Summary (rpm) 2010-09-18

    In development, will always encounter a variety of Chinese garbage problem, the following brief summary. 1, the database garbled Garbled question, our first response should be: if the database character set encoding problem. To mysql, for example, my

  • IOCP model summary (rpm) 2011-09-23

    IOCP (I / O Completion Port, I / O completion ports) is the best kind of performance I / O model. It is the application uses the thread pool handle asynchronous I / O requests of a mechanism. In dealing with multiple concurrent asynchronous I / O req

  • ORACLE ORDER BY Usage Summary (rpm) 2010-10-08

    1, ORDER BY processing on NULL Default processing, Oracle in the Order by the time that the null is a maximum, so if it is ranked in ascending order ASC final, DESC descending order will be placed on top. Of course, you can also use the nulls first o

  • java serialization and deserialization Summary (rpm) 2010-11-13

    The Java objects into a sequence of bytes is known as object serialization. Java byte sequence back to the object is called deserialization of objects. java serialization mechanism is introduced mainly to support two important technologies: RMI and J

  • Sorting algorithm summary (rpm) 2010-11-30

    First, a disorder known bubble sort the data a [1], a [2], ... ... a [n], must be in ascending order. First compare a [1] and a [2] the value, if a [1] over a [2] the value of the exchange between the two, otherwise unchanged. Then compare a [2] and

  • Android - Intent Action Summary (rpm) 2010-12-10

    String ADD_SHORTCUT_ACTION action: in the system to add a shortcut. . "Android.intent.action.ADD_SHORTCUT" String ALL_APPS_ACTION Action: List all available applications. Input: None. "Android.intent.action.ALL_APPS" String ALTERNATIVE

  • dwr Configuration Summary (rpm) 2010-12-23

    A, dwr configuration chapter of the web.xml 1, the minimum configuration <servlet> <servlet-name> dwr-invoker </ servlet-name> <servlet-class> uk.ltd.getahead.dwr.DWRServlet </ servlet-class> </ Servlet> <servlet-map

  • Achieved by using Java byte numeric string length of the interception in the way the English summary (rpm) 2011-01-04

    http://struts2.group.javaeye.com/group/blog/343761

  • Flex Learning Site Summary (rpm) 2011-01-10

    First, the foreign site 1. Resource Adobe Flex 2 Component Explorer: official, show the usage of various components, the entry must see. CFlex: Flex a good resource site, including tutorials, news and resource site page ... ... just a little bit mess

  • Android development environment installation failure under win7 Summary (rpm) 2011-01-11

    Android development environment need: Eclipse, Java environment, ADT, android SDK. Download every software, and detailed installation process, you can refer to various articles online. I had installed the Android development environment, because when

  • Have time to see what oracle-review summary (rpm) 2010-10-26

    One. Oracle database data types commonly used varchar2 (length) variable length strings char (length) length number () indicates the integer or floating-point number (8) number (8,2) clog character large object blog binary large object II. Database q

  • PL / SQL Developer with skills summary (rpm) 2011-04-24

    The following tips only for PL / SQL Developer 6 or later, version 5 only one part of the property. 1, right-click menu in PL / SQL Developer (below referred to as PLD) in a text editor window for each, such as the SQL Window, Command Window and Porg

  • oracle related Summary (rpm) 2011-06-12

    Article from: http://database.group.iteye.com/group/topic/10294 Do not know who the original author had wanted to mark the original source URL, sorry. One. Oracle database data types commonly used varchar2 (length) variable length strings char (lengt

  • Since the database used to create methods by field summary (rpm) 2011-07-01

    DB2 CREATE TABLE T1 ( id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER), ... ); Oracle (need to create a SEQUENCE, and a TRIGGER): CREATE TABLE T1 ( id NUMBER (10,0) NOT NULL

  • Introduction and application of the RPM 2011-01-19

    Introduction and application of RPM Author: North-South-North from: LinuxSir.Org Summary: RPM is the abbreviation for Red Hat Package Manager, originally Red Hat Package Manager; This article describes the RPM, and with examples to explain the RPM ma