java常量设置的方式

时间:2014-11-14 11:59:32   收藏:0   阅读:206

我们在写java程序的时候,常常有常量设置,如:

  

1 public interface Const {
2 
3     //性别的常量
4     public interface Sex{
5         public final int 男=1;//
6         public final int 女=2;//
7     }
8     
9 }

这种设置方法通过接口向外发布常量。但是却有不足之处。它在类型安全与使用方面没有任何帮助。

bubuko.com,布布扣

因此,我们可以采用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 }

 

原文:http://www.cnblogs.com/huzi007/p/4096719.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!