Java-常量-引申-定义常量

首先看示例: 

  1. /** 
  2.  * Method One 
  3.  */  
  4. interface ConstantInterface {  
  5.     String SUNDAY = "SUNDAY";  
  6.     String MONDAY = "MONDAY";  
  7.     String TUESDAY = "TUESDAY";  
  8.     String WEDNESDAY = "WEDNESDAY";  
  9.     String THURSDAY = "THURSDAY";  
  10.     String FRIDAY = "FRIDAY";  
  11.     String SATURDAY = "SATURDAY";  
  12. }  

 

  1. /** 
  2.  * Method Two  
  3.  */  
  4. enum ConstantEnum {  
  5.     SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY  
  6. }  

 

  1. /** 
  2.  * Method Three 
  3.  */  
  4. class ConstantClassField {  
  5.     public static final String SUNDAY = "SUNDAY";  
  6.     public static final String MONDAY = "MONDAY";  
  7.     public static final String TUESDAY = "TUESDAY";  
  8.     public static final String WEDNESDAY = "WEDNESDAY";  
  9.     public static final String THURSDAY = "THURSDAY";  
  10.     public static final String FRIDAY = "FRIDAY";  
  11.     public static final String SATURDAY = "SATURDAY";  
  12. }  

 

  1. /** 
  2.  * Method Four 
  3.  * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html 
  4.  */  
  5. class ConstantClassFunction {  
  6.     private static final String SUNDAY = "SUNDAY";  
  7.     private static final String MONDAY = "MONDAY";  
  8.     private static final String TUESDAY = "TUESDAY";  
  9.     private static final String WEDNESDAY = "WEDNESDAY";  
  10.     private static final String THURSDAY = "THURSDAY";  
  11.     private static final String FRIDAY = "FRIDAY";  
  12.     private static final String SATURDAY = "SATURDAY";  
  13.     public static String getSunday() {  
  14.         return SUNDAY;  
  15.     }  
  16.     public static String getMonday() {  
  17.         return MONDAY;  
  18.     }  
  19.     public static String getTuesday() {  
  20.         return TUESDAY;  
  21.     }  
  22.     public static String getWednesday() {  
  23.         return WEDNESDAY;  
  24.     }  
  25.     public static String getThursday() {  
  26.         return THURSDAY;  
  27.     }  
  28.     public static String getFirday() {  
  29.         return FRIDAY;  
  30.     }  
  31.     public static String getSaturday() {  
  32.         return SATURDAY;  
  33.     }  
  34. }  
  35. public class TestConstant {  
  36.     static final String day = "saturday";  
  37.     public static void main(String[] args) {  
  38.         System.out.println("Is today Saturday?");  
  39.         System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY));  
  40.         System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name()));  
  41.         System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY));  
  42.         System.out.println(day.equalsIgnoreCase(ConstantClassFunction  
  43.                 .getSaturday()));  
  44.     }  
  45. }  

 

 

方法一采用接口(Interface)的中变量默认为static final的特性。

方法二采用了Java 5.0中引入的Enum类型。

方法三采用了在普通类中使用static final修饰变量的方法。

方法四类似方法三,但是通过函数来获取常量。

 

首先定义全局变量似乎有违Java的面向对象的封装特性,增加的耦合。所以最佳的方法是避免定义全局变量。如果是参数等,可以写入配置文件。如果实在是必须的,方法二是最为推荐的。方法三是大家都能想到的,非常的直观。方法一和方法三本质上一样。方法四提供了灵活性。

posted @ 2015-03-03 13:51  luanzh  阅读(149)  评论(0)    收藏  举报