歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 如何在Java中定義常量(Constant)

如何在Java中定義常量(Constant)

日期:2017/3/1 10:22:18   编辑:Linux編程

首先看示例:

  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. }

方法一采用接口(Interface)的中變量默認為static final的特性。

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

方法三采用了在普通類中使用static final修飾變量的方法。

方法四類似方法三,但是通過函數來獲取常量。

首先定義全局變量似乎有違Java的面向對象的封裝特性,增加的耦合。所以最佳的方法是避免定義全局變量。如果是參數等,可以寫入配置文件。如果實在是必須的,方法二是最為推薦的。方法三是大家都能想到的,非常的直觀。方法一和方法三本質上一樣。方法四提供了靈活性,具體參考引用【1】。

參考

【1】http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html

Copyright © Linux教程網 All Rights Reserved