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