Java常量文件

发布于 2024-09-26 16:18:42 字数 105 浏览 2 评论 0 原文

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

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

谢谢。

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

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄情伤 2024-10-03 16:18:42

在 java 和大多数面向对象语言中,仅仅定义一个类来保存常量被认为是不好的做法。最好在与它们关联的类中定义常量。通常有一个。例如,

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

如果确实没有,请随意定义一个单独的类。

编辑:这里的关键是:

  1. 使常量易于查找。如果有一个“自然”的地方可以放置它们,则将它们放置在那里(即 Component 对象的默认高度属于 Component 类)。
  2. 不要有比您需要的更高的耦合。将所有常量放在一个“常量”类中会造成高度耦合,特别是当后续修饰符倾向于将所有常量放入“常量”类中时,无论是否有另一个类可以自然地放入它们。
  3. 仅仅因为使用了常量由多个类组成并不意味着它应该属于“常量”类。如果“Application”和使用 Application 类的类使用常量,则将其放入 Application 类中。这样你就不会增加耦合。

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.

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 class then put it in the Application class. That way you are not increasing the coupling.
幽蝶幻影 2024-10-03 16:18:42

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

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

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

String inst = Constants.SOME_TEXT;

Normally, you'd use a Constants class, or define them in classes where they are used, a 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;
别念他 2024-10-03 16:18:42

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

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

除了私有之外,还可以将其声明为默认,受保护或公开。尽管它被认为是面向对象的反模式来定义常量,但它是一个特殊的“常量”(< a href="http://en.wikipedia.org/wiki/God_object" rel="noreferrer">God) 类,用于存储整个应用程序的常量。或者,您还可以将配置数据存储在 Java 属性文件 中,这不被视为反模式。

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

class Example {
  String filename;

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

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

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 pattern to 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.

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 Guice DI framework:

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. This way, you have minimal coupling and classes that can be independently tested.

刘备忘录 2024-10-03 16:18:42

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

单个 Java 枚举器可以保存关联数据的多个字段。

Oracle 提供了Java 枚举简介

You can define some constants in Java enumerations.

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

Oracle provides this introduction to Java enumerations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文