我们在写java程序的时候,常常有常量设置,如:
1 public interface Const {
2
3 //性别的常量
4 public interface Sex{
5 public final int 男=1;
//男
6 public final int 女=2;
//女
7 }
8
9 }
这种设置方法通过接口向外发布常量。但是却有不足之处。它在类型安全与使用方面没有任何帮助。
因此,我们可以采用java提供的枚举类来实现常量的设置。
以上的可以修改为:
1 public interface Const {
2
3 //性别的常量
4 public enum Sex{
5 男(1),女(2
);
6 private int i;
7 private Sex(
int i){
8 this.i=
i;
9 }
10 public int getI() {
11 return i;
12 }
13
14 }
15
16 }
转载于:https://www.cnblogs.com/huzi007/p/4096719.html