Enum features
1. Use enum to define an enumeration class that inherits the java.lang.Enum class by default instead of the Object class. Among them, the java.lang.Enum class implements two interfaces: java.lang.Serializable and java.lang.Comparable.
2. The constructor of an enumeration class can only use the private access modifier. If the access control modifier of its constructor is omitted, the private modification is used by default;
3. All instances of the enumeration class must be explicitly listed in the enumeration class, otherwise this enumeration class will never be able to generate instances. When these instances are listed, the system automatically adds public static final modifications without the need for programmers to add them explicitly.
},THUR{
public String toLocaleString(){
return "Thursday";
}
},FRI{
public String toLocaleString(){
return "Friday";
}
},SAT{
public String toLocaleString(){
return "Saturday";
}
},SUN{
public String toLocaleString(){
return "Sunday";
}
};
public abstract String toLocaleString();
}
int compareTo method
String name() returns the name of the enumeration instance
int ordinal() returns the index of the enumeration value in the enumeration
String toString() returns the instance name of the enumeration which is more commonly used than name
public static valueOf()
}
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
/*Whether the current light is green*/
private boolean lighted;
/*The corresponding direction that the current light is green at the same time*/
private String opposite;
/*The next light to turn green when the current light turns red*/
private String next;
public boolean isLighted(){
return lighted;
}
/**
* When a certain light turns green, the light in its corresponding direction also turns green.
*/
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
System.out.println(name() + "The lamp is green, there should be a total of 6 directions below where you can see cars passing through!");
}
/**
* When a certain light turns red, the light in the corresponding direction also turns red, and the light in the next direction turns green
* @return The next light to turn green
*/
public Lamp blackOut(){
this.lighted = false;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp= null;
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println("The green light switches from " + name() + "--------> to " + next);
nextLamp.light();
}
return nextLamp;
}
}