enumerate
The introduction of enumerated (enum simulation)
class Student {
private int restDay;
public int getRestDay() {
return restDay;
}
public void setRestDay(int restDay) {
this.restDay = restDay;
}
}
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setRestDay(1);
int res = stu.getRestDay();
if (res == 6 || res == 7) {
System.out.println("放假");
} else {
System.out.println("上课");
}
}
}
The above code, there are some problems:
(1) data is insecure, input 8
stu.setRestDay(8); // 输出:上课(没有星期八)
(2) business logic of the irrational: the first day of the week is not the same as foreign, China is the first day on Monday, the first day of a foreign Zhou.
After the improvements:
class WeekDay {
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;
}
class Student {
private int restDay;
public int getRestDay() {
return restDay;
}
public void setRestDay(int restDay) {
this.restDay = restDay;
}
}
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setRestDay(WeekDay.MONDAY);
int res = stu.getRestDay();
if (res == 6 || res == 7) {
System.out.println("放假");
} else {
System.out.println("上课");
}
}
}
The improved code, there are problems:
(1) data is insecure, input 8
stu.setRestDay(8); // 输出:上课(没有星期八)
Improved again:
class WeekDay {
private WeekDay() {}
public static final WeekDay MONDAY = new WeekDay();
public static final WeekDay TUESDAY = new WeekDay();
public static final WeekDay WEDNESDAY = new WeekDay();
public static final WeekDay THURSDAY = new WeekDay();
public static final WeekDay FRIDAY = new WeekDay();
public static final WeekDay SATURDAY = new WeekDay();
public static final WeekDay SUNDAY = new WeekDay();
}
class Student {
private WeekDay restDay;
public WeekDay getRestDay() {
return restDay;
}
public void setRestDay(WeekDay restDay) {
this.restDay = restDay;
}
}
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setRestDay(WeekDay.MONDAY);
WeekDay res = stu.getRestDay();
if (res == WeekDay.SATURDAY || res == WeekDay.SUNDAY) {
System.out.println("放假");
} else {
System.out.println("上课");
}
}
}
The code above solves the problem, but the code does not look good, too redundant.
What is an enumeration
Enumeration: Indicates a fixed state of things.
For example: season (spring, summer, autumn, winter), day of week (Monday to Sunday), gender (male and female)
The definition of enumeration
format:
[修饰符] enum 枚举的名称 {
常量1,常量2,常量3……
}
such as:
enum Sex {
MAN, FEMALE
}
Enumeration of nature
java enum is a syntax sugar . Is a special class, a collection of objects is more constants.
When we define an enumeration, its essence is a class, a class that inherits Enum.
enum Sex {
MAN, FEMALE
}
Decompile after:
final class Sex extends Enum
{
public static final Sex MAN;
public static final Sex FEMALE;
private static final Sex ENUM$VALUES[];
private Sex(String s, int i)
{
super(s, i);
}
public static Sex[] values()
{
Sex asex[];
int i;
Sex asex1[];
System.arraycopy(asex = ENUM$VALUES, 0, asex1 = new Sex[i = asex.length], 0, i);
return asex1;
}
public static Sex valueOf(String s)
{
return (Sex)Enum.valueOf(Test/Sex, s);
}
static
{
MAN = new Sex("MAN", 0);
FEMALE = new Sex("FEMALE", 1);
ENUM$VALUES = (new Sex[] {
MAN, FEMALE
});
}
}