首页 > 代码库 > 转载--创建java常量类的方法
转载--创建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 } 13 /** 14 * Method Two 15 */ 16 enum ConstantEnum { 17 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 18 } 19 /** 20 * Method Three 21 */ 22 class ConstantClassField { 23 public static final String SUNDAY = "SUNDAY"; 24 public static final String MONDAY = "MONDAY"; 25 public static final String TUESDAY = "TUESDAY"; 26 public static final String WEDNESDAY = "WEDNESDAY"; 27 public static final String THURSDAY = "THURSDAY"; 28 public static final String FRIDAY = "FRIDAY"; 29 public static final String SATURDAY = "SATURDAY"; 30 } 31 /** 32 * Method Four 33 * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html 34 */ 35 class ConstantClassFunction { 36 private static final String SUNDAY = "SUNDAY"; 37 private static final String MONDAY = "MONDAY"; 38 private static final String TUESDAY = "TUESDAY"; 39 private static final String WEDNESDAY = "WEDNESDAY"; 40 private static final String THURSDAY = "THURSDAY"; 41 private static final String FRIDAY = "FRIDAY"; 42 private static final String SATURDAY = "SATURDAY"; 43 public static String getSunday() { 44 return SUNDAY; 45 } 46 public static String getMonday() { 47 return MONDAY; 48 } 49 public static String getTuesday() { 50 return TUESDAY; 51 } 52 public static String getWednesday() { 53 return WEDNESDAY; 54 } 55 public static String getThursday() { 56 return THURSDAY; 57 } 58 public static String getFirday() { 59 return FRIDAY; 60 } 61 public static String getSaturday() { 62 return SATURDAY; 63 } 64 } 65 public class TestConstant { 66 static final String day = "saturday"; 67 public static void main(String[] args) { 68 System.out.println("Is today Saturday?"); 69 System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY)); 70 System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name())); 71 System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY)); 72 System.out.println(day.equalsIgnoreCase(ConstantClassFunction 73 .getSaturday())); 74 } 75 } 76 [java] view plain copy print? 77 /** 78 * Method One 79 */ 80 interface ConstantInterface { 81 String SUNDAY = "SUNDAY"; 82 String MONDAY = "MONDAY"; 83 String TUESDAY = "TUESDAY"; 84 String WEDNESDAY = "WEDNESDAY"; 85 String THURSDAY = "THURSDAY"; 86 String FRIDAY = "FRIDAY"; 87 String SATURDAY = "SATURDAY"; 88 } 89 /** 90 * Method Two 91 */ 92 enum ConstantEnum { 93 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 94 } 95 /** 96 * Method Three 97 */ 98 class ConstantClassField { 99 public static final String SUNDAY = "SUNDAY"; 100 public static final String MONDAY = "MONDAY"; 101 public static final String TUESDAY = "TUESDAY"; 102 public static final String WEDNESDAY = "WEDNESDAY"; 103 public static final String THURSDAY = "THURSDAY"; 104 public static final String FRIDAY = "FRIDAY"; 105 public static final String SATURDAY = "SATURDAY"; 106 } 107 /** 108 * Method Four 109 * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html 110 */ 111 class ConstantClassFunction { 112 private static final String SUNDAY = "SUNDAY"; 113 private static final String MONDAY = "MONDAY"; 114 private static final String TUESDAY = "TUESDAY"; 115 private static final String WEDNESDAY = "WEDNESDAY"; 116 private static final String THURSDAY = "THURSDAY"; 117 private static final String FRIDAY = "FRIDAY"; 118 private static final String SATURDAY = "SATURDAY"; 119 public static String getSunday() { 120 return SUNDAY; 121 } 122 public static String getMonday() { 123 return MONDAY; 124 } 125 public static String getTuesday() { 126 return TUESDAY; 127 } 128 public static String getWednesday() { 129 return WEDNESDAY; 130 } 131 public static String getThursday() { 132 return THURSDAY; 133 } 134 public static String getFirday() { 135 return FRIDAY; 136 } 137 public static String getSaturday() { 138 return SATURDAY; 139 } 140 } 141 public class TestConstant { 142 static final String day = "saturday"; 143 public static void main(String[] args) { 144 System.out.println("Is today Saturday?"); 145 System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY)); 146 System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name())); 147 System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY)); 148 System.out.println(day.equalsIgnoreCase(ConstantClassFunction 149 .getSaturday())); 150 } 151 }
方法一采用接口(Interface)的中变量默认为static final的特性。
方法二采用了Java 5.0中引入的Enum类型。
方法三采用了在普通类中使用static final修饰变量的方法。
方法四类似方法三,但是通过函数来获取常量。
首先定义全局变量似乎有违Java的面向对象的封装特性,增加的耦合。所以最佳的方法是避免定义全局变量。如果是参数等,可以写入配置文件。如果实在是必须的,方法二是最为推荐的。方法三是大家都能想到的,非常的直观。方法一和方法三本质上一样。方法四提供了灵活性,具体参考引用【1】
转载--创建java常量类的方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。