【问题标题】:Java constant examples (Create a java file having only constants)Java 常量示例(创建一个只有常量的 java 文件)
【发布时间】:2012-09-13 03:14:56
【问题描述】:

声明只有常量的 java 文件的最佳做法是什么?

public interface DeclareConstants
{
    String constant = "Test";
}

public abstract class DeclareConstants
{
    public static final String constant = "Test";
}

【问题讨论】:

  • 常数变量,这不是矛盾吗?
  • @PeterWalser 所以是一个 static 变量。 :D 从技术上讲,静态意味着与不变但仍然不同的东西......

标签: java constants


【解决方案1】:

一个都没有。使用final class for Constants 将它们声明为public static final 并在必要时静态导入所有常量。

public final class Constants {

    private Constants() {
            // restrict instantiation
    }

    public static final double PI = 3.14159;
    public static final double PLANCK_CONSTANT = 6.62606896e-34;
}

用法:

import static Constants.PLANCK_CONSTANT;
import static Constants.PI;//import static Constants.*;

public class Calculations {

        public double getReducedPlanckConstant() {
                return PLANCK_CONSTANT / (2 * PI);
        }
}

查看维基链接:http://en.wikipedia.org/wiki/Constant_interface

【讨论】:

  • @Gilberto : Wiki 链接描述得很好:)
  • 只有在实现接口时才成为反模式。如果您对强制执行此用法很讲究,请不要忘记该类的私有 final 构造函数。
  • 只要将构造函数设为私有就足够了; final 不是必需的,因为您不能从没有可见构造函数的类继承。
  • 很好地描述了我正在寻找的东西
  • 谢谢,这有助于消除我的困惑。
【解决方案2】:

- 创建一个带有public static final 字段Class

-然后您可以使用Class_Name.Field_Name从任何类访问这些字段。

-可以将class声明为final,这样class不能扩展(继承)和修改......

【讨论】:

    【解决方案3】:

    两者都有效,但我通常选择接口。如果没有实现,则不需要类(无论是否抽象)。

    作为建议,请尝试明智地选择常量的位置,它们是您的外部合同的一部分。不要将每个常量都放在一个文件中。

    例如,如果一组常量仅在一个类或一个方法中使用,则将它们放在该类、扩展类或实现的接口中。如果您不小心,最终可能会导致严重的依赖混乱。

    有时枚举是常量(Java 5)的一个很好的替代品,请看: http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html

    【讨论】:

    • 不客气。我真的很喜欢最后一堂课的解决方案!! (假证明)
    【解决方案4】:

    这个问题很老了。但我想提一下另一种方法。 使用枚举来声明常量值。 根据 Nandkumar Tekale 的回答,枚举可以按如下方式使用:

    枚举:

    public enum Planck {
        REDUCED();
        public static final double PLANCK_CONSTANT = 6.62606896e-34;
        public static final double PI = 3.14159;
        public final double REDUCED_PLANCK_CONSTANT;
    
        Planck() {
            this.REDUCED_PLANCK_CONSTANT = PLANCK_CONSTANT / (2 * PI);
        }
    
        public double getValue() {
            return REDUCED_PLANCK_CONSTANT;
        }
    }
    

    客户端类:

    public class PlanckClient {
    
        public static void main(String[] args) {
            System.out.println(getReducedPlanckConstant());
            // or using Enum itself as below:
            System.out.println(Planck.REDUCED.getValue());
        }
    
        public static double getReducedPlanckConstant() {
            return Planck.PLANCK_CONSTANT / (2 * Planck.PI);
        }
    
    }
    

    参考: Joshua Bloch 在他的 Effective Java 书中建议使用枚举来声明常量字段。

    【讨论】:

      【解决方案5】:

      你也可以使用属性类

      这里是常量文件

      # this will hold all of the constants
      frameWidth = 1600
      frameHeight = 900
      

      这是使用常量的代码

      public class SimpleGuiAnimation {
      
          int frameWidth;
          int frameHeight;
      
      
          public SimpleGuiAnimation() {
              Properties properties = new Properties();
      
              try {
                  File file = new File("src/main/resources/dataDirectory/gui_constants.properties");
                  FileInputStream fileInputStream = new FileInputStream(file);
                  properties.load(fileInputStream);
              }
              catch (FileNotFoundException fileNotFoundException) {
                  System.out.println("Could not find the properties file" + fileNotFoundException);
              }
              catch (Exception exception) {
                  System.out.println("Could not load properties file" + exception.toString());
              }
      
      
              this.frameWidth = Integer.parseInt(properties.getProperty("frameWidth"));
              this.frameHeight = Integer.parseInt(properties.getProperty("frameHeight"));
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        相关资源
        最近更新 更多