Java 常量文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3866190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:01:46  来源:igfitidea点击:

Java constants file

javaandroidconstants

提问by VansFannel

I'm developing an Android application and I'm very new on Java and Android.

我正在开发一个 Android 应用程序,我对 Java 和 Android 非常陌生。

I want to create some constants to use in some activities. Where can I define these constants?

我想创建一些常量以在某些活动中使用。我在哪里可以定义这些常量?

Thanks.

谢谢。

采纳答案by DJClayworth

It's considered bad practice in java, and most OO languages, to define a class simply to hold constants. It's much better to define the constants in a class they are associated with. Usually there is one. e.g.

在 java 和大多数 OO 语言中,定义一个类来简单地保存常量被认为是不好的做法。最好在与之关联的类中定义常量。通常有一个。例如

interface MyComponent {
  /** The default height for a component */
  public static final int DEFAULT_HEIGHT = 5;
  // other stuff
}

If there really isn't one feel free to define a separate class.

如果真的没有人可以随意定义一个单独的类。

EDIT:The key things here are:

编辑:这里的关键是:

  1. Make the constants easy to find. If there is a 'natural' place to put them, put them there (i.e. the default height for Component objects belong in the Component class).
  2. Don't have higher coupling than you need to. Putting all your constants in one 'Constants' class makes for high coupling, especially as subsequent modifiers tend to then put ALL constants in the Constants class, whether or not there is another class they could naturally be put in.
  3. Just because a constant is used by more than one class that doesn't mean it should be in a 'Constants' class. If a constant is used by 'Application' and classes that use the Application classthen put it in the Application class. That way you are not increasing the coupling.
  1. 使常量易于查找。如果有放置它们的“自然”位置,请将它们放在那里(即 Component 对象的默认高度属于 Component 类)。
  2. 不要有比你需要的更高的耦合。将所有常量放在一个“常量”类中会导致高耦合,特别是因为随后的修饰符倾向于将所有常量放在 Constants 类中,无论是否有另一个类可以自然地放入它们。
  3. 仅仅因为一个常量被多个类使用,并不意味着它应该在“常量”类中。如果“Application”和使用 Application 类的类使用常量则将其放入 Application 类中。这样你就不会增加耦合。

回答by billjamesdev

Normally, you'd use a Constants class, or define them in classes where they are used, a la:

通常,您会使用 Constants 类,或者在使用它们的类中定义它们,例如:

class Constants {
   public static final int NUM_TRIANGLES = 4;
   public static final String SOME_TEXT = "This is a constant";
}

Then you'd refer to them by:

然后你可以通过以下方式引用它们:

String inst = Constants.SOME_TEXT;

回答by Andy Thomas

You can define some constants in Java enumerations.

您可以在 Java 枚举中定义一些常量。

A single Java enumerator may hold multiple fields of associated data.

单个 Java 枚举器可能包含关联数据的多个字段。

Oracle provides this introduction to Java enumerations.

Oracle 提供了对 Java 枚举的介绍。

回答by Kdeveloper

The most common way is to create 'constants' in the classes were you need them:

最常见的方法是在需要它们的类中创建“常量”:

class Example { 
  private static final int FILENAME = "test.txt; 
} 

Instead of private it can also be declared default, protected or public. Although it is considered an OO anti patternto define constants is a special 'constants' (God) class that stores constants for the whole application. Alternatively, you can also store configuration data in a Java properties file, this is not considered an anti-pattern.

除了私有之外,它还可以声明为default、 protected 或 public 。尽管定义常量被认为是 OO反模式,但它是一个特殊的“常量”(上帝)类,用于存储整个应用程序的常量。或者,您也可以将配置数据存储在Java 属性文件中,这不被视为反模式。

Another option, that is rapidly gaining popularity, is the usage of the Dependency Inject(DI) pattern. Often this pattern is used for depend object, but it can also be used to inject constant values into objects. This can for example be implemented with Google's lightweight GuiceDI framework:

另一种迅速流行的选择是使用依赖注入(DI) 模式。这种模式通常用于依赖对象,但它也可用于将常量值注入对象。例如,这可以使用 Google 的轻量级GuiceDI 框架来实现:

class Example {
  String filename;

  @Inject
  public Example(@ConfigFilename String filename) {
     this.filename = filename;        
  }

In a special Binderclass you will bind a value to the Strings annotated with @ConfigFilename. This way, you have minimal coupling and classes that can be independently tested.

在一个特殊的Binder类中,您将一个值绑定到用 @ConfigFilename 注释的字符串。通过这种方式,您可以拥有最少的耦合和可以独立测试的类。