java1.5 enumeration implementation principle of the new features

2009-12-18  来源:本站原创  分类:Java  人气:214 

package com.bp_soft.day01;
/**
 * Java1.5 New features in how enumeration
 *  Here I write a normal class to simulate the enumeration of the implementation process
 * @author kevin
 * 2010-3-8
 */
public abstract class TrafficLight {
        // Use an enum, defined constants is actually an object of the current class static constants
        //(1) With an ordinary class simulation enumeration, first class constructor should be private
        private int time;

        private TrafficLight(){};

        private TrafficLight(int time){
                this.time=time;
        }
        // Define the RED, GREEN, YELLOW current class's static constant object
        public final static TrafficLight RED=new TrafficLight(30){

                @Override
                public TrafficLight nextLight() {
                        return GREEN;
                }

                @Override
                public String toString() {
                        return "RED";
                }

        };

        public final static TrafficLight GREEN=new TrafficLight(45){

                @Override
                public TrafficLight nextLight() {
                        return YELLOW;
                }

                @Override
                public String toString() {
                        return "YELLOW";
                }

        };
        // This is done by anonymous inner implemented within
        public final static TrafficLight YELLOW=new TrafficLight(){

                @Override
                public TrafficLight nextLight() {
                        return RED;
                }

                @Override
                public String toString() {
                        return "RED";
                }

        };

        // When we use this kind of time, not just to create other objects  ,( We can only choose the above three objects one  ), This is thought to enumerate implementation
        public abstract String toString();
        // The parent class is a subclass of the abstract methods to implement
        public abstract TrafficLight nextLight();

        public static void main(String args[]){
                System.out.println(RED.nextLight());
        }
}
package com.bp_soft.day01;
/**
 *  Here I write a enumeration, compare you know that the enumeration has come to implement principle
 * @author kevin
 * 2010-3-8
 */
public enum TrafficLightEnum {

        RED(30){
                public  TrafficLightEnum  nextLight(){
                        return GREEN;
                }

                public  String toString(){
                        return "RED";
                }
        },GREEN(45){
                public  TrafficLightEnum  nextLight(){
                        return YELLOW;
                }

                public  String toString(){
                        return "GREEN";
                }
        },YELLOW(5){
                public  TrafficLightEnum  nextLight(){
                        return YELLOW;
                }

                public  String toString(){
                        return "YELLOW";
                }
        };
        private int time; 

        private TrafficLightEnum(){}// In the enumeration to define private construction methods  

        private TrafficLightEnum(int time){this.time=time;}// The enumeration enables you to define multiple private constructor  

        public abstract TrafficLightEnum  nextLight();

        public abstract String toString();
}
相关文章
  • java1.5 enumeration implementation principle of the new features 2009-12-18

    package com.bp_soft.day01; /** * Java1.5 New features in how enumeration * Here I write a normal class to simulate the enumeration of the implementation process * @author kevin * 2010-3-8 */ public abstract class TrafficLight { // Use an enum, define

  • No website MSN Robot rice Implementation Principle and Code Dotmsn2.0 2010-04-12

    "Transfer from http://tb.blog.csdn.net/TrackBack.aspx?PostId=1749348 Authors: funy> No rice, jack distortion are MSN, QQ, GTalk Robot Implementation Principle and Code - No rice, jack distortion MSN Robot implementation principle (DotMSN2.0 analyt

  • (Turn) Java biased locking implementation principle (Biased Locking) 2010-03-21

    http://www.javaeye.com/topic/518066 Read this article readers, the need for Java lightweight locks have a certain understanding, know the lock record, mark word like terms. Can refer to my blog post: Java Lightweight lock principle Xiang Jie (Lightwe

  • C # achieve QQ automatically log device implementation principle 2010-04-11

    Seen in CSDN, Wang Science Teacher published, I feel so deeply, so reserved a moment to share. Implementation code private void QQLogin (string QQ, string Pwd) (System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceP

  • EJB2.0 Tutorial Xiangjie EJB technology and implementation principle (1) 2010-06-14

    EJB2.0 Tutorial Xiangjie EJB technology and implementation principle (1) http://developer.51cto.com 2009-06-11 16:25 IT technology I want to comment (0) This article introduces you to the remote EJB is called javaBean. As well as details of the EJB2.

  • Database connection pool implementation principle 2010-09-21

    Database connection pool implementation principle Implementation principle is to use dynamic proxy technology Write connection pool required to achieve java.sql.DataSource interface DataSource interface defines two overloaded getConnection method: Co

  • java HashMap implementation principle [Z] 2010-10-12

    In-depth collection of Java Learning Series: HashMap implementation principle 1. HashMap Overview: HashMap Hash table based on asynchronous Map interface implementation. This implementation provides all optional map operations, and allows the use of

  • In-depth collection of Java Learning Series: HashMap implementation principle (to) 2010-10-29

    In-depth collection of Java Learning Series: HashMap implementation principle Articles Category: Java Programming 1. HashMap Overview: HashMap Hash table based on asynchronous Map interface implementation. This implementation provides all optional ma

  • Rsync implementation principle 2011-05-12

    Rsync implementation principle Preface The original document on the rsync Rsync technical report , and Andrew Tridgell's paper Phd thesis (pdf) on the rsync algorithm principle is an excellent document. However, these documents focus on the rsync alg

  • [Reprinted] WordPress plugins implementation principle 2010-09-19

    http://www.efish.tk/wordpress-plugins.html Has been the software components, plug-in architecture is very interesting, I think that developing any application, must have a good plug-in system architecture, this can attract other people to develop plu

  • Implementation principle of MySQL Replication 2010-10-26

    To make good use of a system, understand its principle is very important to achieve something, only to understand the principles of its realization, we can exceed and rational use, to be able to build the most suitable system of our application envir

  • yale Cas implementation principle and the basic protocols 2010-11-03

    CAS (Central Authentication Service) is a Yale University launched an open source project, according to statistics, about every 10 Web SSO using the Java open-source project built, there are eight to use CAS. These statistics, although I disagree, bu

  • Yale CAS SSO implementation principle and foundation agreement 2011-04-13

    CAS (Central Authentication Service) is a Yale University launched an open source project, according to statistics, about every 10 to build Web SSO using open source Java project, there are eight to use CAS. These statistics, although I disagree, but

  • [Turn] the level of segmentation of the database implementation principle analysis --- sub-libraries, points table, master, clusters, load balancers 2011-05-21

    Chapter 1 Introduction With the widespread popularity of Internet applications, massive data storage and access system design has become a bottleneck. For a large-scale Internet applications, every day billions of PV will undoubtedly cause a very hig

  • Struts 1 implementation principle of my understanding 2008-07-15

    Struts is a servlet based on the design of a framework. See the following section to configure the configuration servlet in web.xml. In its configuration file, we can see that the container will visit the WEB address for each one corresponding to a s

  • Implementation principle of the shopping cart 2010-06-04

    Equivalent to the real supermarket shopping cart shopping cart, is the physical difference is a car, a virtual car only. Users can shopping site to jump between different pages to choose their favorite product, click on the time of purchase, the prod

  • Struts2 in the interceptor stack implementation principle 2010-09-14

    1 Interceptor Interface public interface Interceptor ( void intercept (ActionInvocation invocation); ) A class for the invocation interceptor ActionInvocation / ** * Save the number of interceptor * @ Author Administrator * * / public class ActionInv

  • (Transfer) the regular expression parser implementation principle 2010-09-18

    Transfer from: http://blog.csdn.net/qipnx/archive/2007/10/22/1836596.aspx Regular expressions can be used: (1) verify whether the string specified characteristics, such as verify that legitimate e-mail address. (2) to find a string, from a long text

  • tomcat implementation principle 2010-09-26

    http://hi.baidu.com/lynuhuoqubing/blog/item/bd05e77a5800c0ea2e73b3be.html Tomcat works turn] [2009-01-02 15:571 - Tomcat Server component 1.1 - Server A Server element represents the entire Catalina servlet container. (Singleton) 1.2 - Service A Serv

  • [Transfer] Tomcat implementation principle of the cluster Cluster Analysis 2010-10-26

    For WEB Application Clusters technology, the biggest difficulty is how multiple nodes in the cluster to maintain data consistency between, session (Session) information is the most important piece of data. To achieve this, generally there are two way