简体   繁体   English

Java常量文件

[英]Java constants file

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. 谢谢。

It's considered bad practice in java, and most OO languages, to define a class simply to hold constants. 在java和大多数OO语言中,仅仅为了保存常量来定义一个类被认为是不好的做法。 It's much better to define the constants in a class they are associated with. 在与它们相关联的类中定义常量要好得多。 Usually there is one. 通常有一个。 eg 例如

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 (ie the default height for Component objects belong in the Component class). 如果有一个“自然”放置它们,请将它们放在那里(即Component对象的默认高度属于Component类)。
  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. 将所有常量放在一个'常量'类中会产生高耦合,尤其是后续修饰符往往会将所有常量放在Constants类中,无论是否还有其他类可以自然放入。
  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 class then put it in the Application class. 如果'Application' 和使用Application类的类使用常量则将其放在Application类中。 That way you are not increasing the coupling. 这样你就不会增加耦合。

Normally, you'd use a Constants class, or define them in classes where they are used, a la: 通常,您使用Constants类,或者在使用它们的类中定义它们,la:

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;

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. 它也可以声明为default ,protected或public,而不是private。 Although it is considered an OO anti pattern to define constants is a special 'constants' ( God ) class that stores constants for the whole application. 虽然定义常量被认为是一种OO 反模式 ,但它是一个特殊的“常量”( 上帝 )类,它存储整个应用程序的常量。 Alternatively, you can also store configuration data in a Java properties file , this is not considered an anti-pattern. 或者,您也可以将配置数据存储在Java属性文件中 ,这不被视为反模式。

Another option, that is rapidly gaining popularity, is the usage of the Dependency Inject (DI) pattern. 另一种迅速普及的选择是使用依赖注入 (DI)模式。 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 Guice DI framework: 例如,可以使用Google的轻量级Guice DI框架实现:

class Example {
  String filename;

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

In a special Binder class you will bind a value to the Strings annotated with @ConfigFilename. 在一个特殊的Binder类中,您将一个值绑定到使用@ConfigFilename注释的字符串。 This way, you have minimal coupling and classes that can be independently tested. 这样,您可以独立测试最小的耦合和类。

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枚举的介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM