Enumeration in jdk1.5 new feature 5: analog Enumeration type

Source: Internet
Author: User

 

Simulation Method 1

 

Package cn. xy. Enum;

Public class TrafficLampEasy
{
Private int time;

Public final static TrafficLampEasy REDLAMP = new TrafficLampEasy (20 );
Public final static TrafficLampEasy GREENLAMP = new TrafficLampEasy (20 );
Public final static TrafficLampEasy YELLOWLAMP = new TrafficLampEasy (5 );

Public TrafficLampEasy ()
{
}

Public TrafficLampEasy (int time)
{
This. time = time;
}

Public TrafficLampEasy nextLamp ()
{
TrafficLampEasy result = new TrafficLampEasy ();

If (this = REDLAMP)
{
Result = GREENLAMP;
}
Else if (this = GREENLAMP)
{
Result = YELLOWLAMP;
}
Else if (this = YELLOWLAMP)
{
Result = REDLAMP;
}
Return result;
}

Public String getValue ()
{
String result = "";

If (this = REDLAMP)
{
Result = "red light, duration:" + time;
}
Else if (this = GREENLAMP)
{
Result = "green light, duration:" + time;
}
Else if (this = YELLOWLAMP)
{
Result = "yellow light, duration:" + time;
}
Return result;
}

Public int getTime ()
{
Return time;
}

Public void setTime (int time)
{
This. time = time;
}
}

 

TrafficLampEasy teRed = TrafficLampEasy. REDLAMP;
System. out. println (teRed. getValue ());
System. out. println (teRed. nextLamp (). getValue ());

From this example, we can see that the enumeration type is actually a class that returns the class itself

 

 

Simulation Method 2

Package cn. xy. Enum;

Public abstract class TrafficLamp
{
/**
* Next light
*/
Public abstract TrafficLamp nextLamp ();

/**
* Get value
*/
Public abstract String getValue ();

/**
* Duration
*/
Private int time;

Public TrafficLamp ()
{
}

Public TrafficLamp (int time)
{
This. time = time;
}

/**
* The red light and Anonymous class are equivalent to inheriting the TrafficLamp abstract class and implementing the abstract method.
*/
Public final static TrafficLamp REDLAMP = new TrafficLamp (50 ){
@ Override
Public TrafficLamp nextLamp ()
{
Return GREENLAMP;
}

@ Override
Public String getValue ()
{
Return "red light, duration:" + this. getTime ();
}
};

Public final static TrafficLamp GREENLAMP = new TrafficLamp (50 ){
@ Override
Public TrafficLamp nextLamp ()
{
Return YELLOWLAMP;
}

@ Override
Public String getValue ()
{
Return "green light, duration:" + this. getTime ();
}
};

Public final static TrafficLamp YELLOWLAMP = new TrafficLamp (2 ){
@ Override
Public TrafficLamp nextLamp ()
{
Return REDLAMP;
}

@ Override
Public String getValue ()
{
Return "yellow light, duration:" + this. getTime ();
}
};

Public int getTime ()
{
Return time;
}

Public void setTime (int time)
{
This. time = time;
}
}

TrafficLamp red = TrafficLamp. REDLAMP;
System. out. println (red. getValue ());
System. out. println (red. nextLamp (). getValue ());

 

What is an anonymous class?

 

Anonymous classes are suitable for those that only need to be used once.

Public abstract class AnonymousClassDesk
{
Public abstract double getPrice ();

Public abstract String getName ();

}

 

Public class Desk extends AnonymousClassDesk
{
@ Override
Public double getPrice ()
{
Return 100;
}

@ Override
Public String getName ()
{
Return "ordinary desk ";
}

}

Public static void main (String [] args)
{
AnonymousClassDesk desk = new AnonymousClassDesk (){

@ Override
Public double getPrice ()
{
Return 100;
}

@ Override
Public String getName ()
{
Return "anonymous desk ";
}
};

System. out. println (desk. getName ());
}

 

Not only can abstract classes, but also interfaces. There is nothing special about the Anonymous class, and the implementation method is also required.

This article from the "IT Xu fat column" blog, please be sure to keep this source http://woshixy.blog.51cto.com/5637578/1059778

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.