1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
例子:定义一个员工类Employees,使用一个变量restday来表示他哪一天休息
 
*/
 
//-------------------------------version:0.1----------------------
/*
class Employees
{
     private int restday; //一周的哪一天休息
 
     public int getRestday()
     {
         return restday;
     }
 
     public void setRestday(int restday)
     {
         this.restday = restday;
     }
}
 
 
public class EnumerateDemo
{
     public static void main(String[] args) 
     {
         Employees e = new Employees(); //创建员工对象
         e.setRestday(2); //设置休息时间
 
         int restday = e.getRestday();
         if(restday == 6 || restday == 7)
         {
             System.out.println("周末休息...");
         }
         else
         {
             System.out.println("周一至周五休息...");
         }
     }
}
 
上述代码的问题:
1、数据类型不安全 (可以传入float等)
2、数据范围不安全 (可以设置任何数字)
3、数据表示的含义不明确 (3表示什么?)
*/
 
 
//----------------------------version:0.2(对发现的问题进行改进)-----------------------------
//定义一个星期几的常量类
/*
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 Employees
{
     private int restday; //一周的哪一天休息
 
     public int getRestday()
     {
         return restday;
     }
 
     public void setRestday(int restday)
     {
         this.restday = restday;
     }
}
 
public class EnumerateDemo
{
     public static void main(String[] args) 
     {
         Employees e = new Employees(); //创建员工对象
         e.setRestday(Weekday.Wednesday); //通过引用常量设置休息时间
     
         int restday = e.getRestday(); //得到值
         if(restday == 6 || restday == 7)
         {
             System.out.println("周末休息...");
         }
         else
         {
             System.out.println("周一至周五休息...");
         }
     }
}
解决的问题:
1、数据表示的含义明确性已经提高,Monday = 1; 表示周一
依然存在问题:
1、数据类型还是不安全
2、数据范围不安全 (还是可以设置任何数字)
*/
 
 
//--------------------------------version:0.3--------------------------------
//将原来的常量类改写:封装成对象的方式
class Weekday
{
     private Weekday(){}; //预防创建对象,私有化构造函数
     public static final Weekday Monday = new Weekday(); //因为int类型不安全,因此改写对象,Monday变量存储的是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 Employees
{
     private Weekday restday; //一周的哪一天休息(数据类型为Weekday)
 
     public Weekday getRestday()
     {
         return restday;
     }
 
     public void setRestday(Weekday restday) //数据类型为Weekday
     {
         this.restday = restday;
     }
}
 
public class EnumerateDemo
{
     public static void main(String[] args) 
     {
         Employees e = new Employees(); //创建员工对象
         e.setRestday(Weekday.Wednesday); //通过类名.对象的方式来设置休息时间
         Weekday restday = e.getRestday(); //用Weekday类型的变量restday来接收得到的值
         
         if(restday == Weekday.Saturday || restday == Weekday.Sunday)
         {
             System.out.println("周末休息...");
         }
         else
         {
             System.out.println("周一至周五休息...");
         }
     }
}
 
/*解决了的问题:
1、数据类型安全了
2、数据范围安全了*/
 
 
 
//--------------------version:4.(引入枚举)---------------------------------
//见JAVA枚举(二),该知识点努力更新中...