English 中文(简体)
如何界定 Java的固定等级?
原标题:How do you define a class of constants in Java?

Suppose you need to define a class which all it does is hold constants.

public static final String SOME_CONST = "SOME_VALUE";

What is the preferred way of doing this?

  1. Interface
  2. Abstract Class
  3. Final Class

我应该使用什么东西?


Clarifications to some answers:

Enums——我不想使用 en,但我没有列举任何东西,只是收集一些不以任何方式相互有关的不变。

<>Interface-Im将不将任何类别列为实施接口的一类。 只想利用接口来打上像样的恒星:ISomeInterface。 SOME_CONST

最佳回答

Use a final class, and define a private constructor to hide the public one.
For simplicity you may then use a static import to reuse your values in another class

public final class MyValues {

  private MyValues() {
    // No need to instantiate the class, we can hide its constructor
  }

  public static final String VALUE1 = "foo";
  public static final String VALUE2 = "bar";
}

在另一类:

import static MyValues.*
//...

if (VALUE1.equals(variable)) {
  //...
}
问题回答

你的澄清说:“如果我不想使用 en,我就没有列举任何东西,只是收集某种不相干的不变”。

If the constants aren t related to each other at all, why do you want to collect them together? Put each constant in the class which it s most closely related to.

我的建议(减少优惠秩序):

1 <>Don t do it。 在实际类别中创造最相关的不变值。 有了一套固定的班级/间歇式,实际上没有遵循联络处的最佳做法。

我和所有其他人不时忽视第1号。 如果你重新这样做的话:

2)final category with private Constructionor 这至少将防止任何人通过扩大/实施固定装置以方便地接触固定装置而滥用你的固定装置。 (我知道你说你不会这样做——但是这并不意味着有人在你赢得胜利后来到这里)

www.un.org/Depts/DGACM/index_french.htm 这将发挥作用,但我不赞成在第2号中提及可能的虐待。

一般而言,由于这些是固定的,并不意味着你仍然适用正常的禁忌原则。 如果只有一年级的人不关心常态,那就应该是私人的,属于私人的。 如果只进行检测,就应当采用测试,而不是生产法。 如果在多个地点(并非偶然相同)界定了固定不变,则重新证明可以消除重复。 因此,他们与你一样对待。

As Joshua Bloch notes in Effective Java:

  • Interfaces should only be used to define types,
  • abstract classes don t prevent instanciability (they can be subclassed, and even suggest that they are designed to be subclassed).

如果你的所有固定点(如地球名称)都相互关联,那么你就可以使用埃南语,在他们与(如果你能够使用)相关的课堂上固定价值,或使用不可贬损的通用物(设计私人违约建筑)。

class SomeConstants
{
    // Prevents instanciation of myself and my subclasses
    private SomeConstants() {}

    public final static String TOTO = "toto";
    public final static Integer TEN = 10;
    //...
}

那么,正如已经指出的那样,你可以使用静态进口来使用你的常数。

我倾向于这样做。 当Java 5引进了几类胎儿时,固定工龄的死亡率相当高。 甚至在此之前,Scott Blach发表了一份(略为多字)版本的版本,该版本在Java1.4(和之前)工作。

除非你们需要一些遗产法典的相互操作性,否则实际上就没有理由再使用所谓的Sting/integer常数。

<代码>enum>/code>s = 罚款。 IIRC, one items in effective Java (2nd Ed) has enumtanting standard options implementing a [Java keyword] interface for any Value.

我倾向于在<条码>限值类别<>上使用“Java关键词]<条码>。 页: 1 一些人将争辩说,<条码>内联网<>。 坏方案者可以执行,但坏方案者将写成法典,不管你做什么。

什么看得更好?

public final class SomeStuff {
     private SomeStuff() {
         throw new Error();
     }
     public static final String SOME_CONST = "Some value or another, I don t know.";
}

Or:

public interface SomeStuff {
     String SOME_CONST = "Some value or another, I don t know.";
}

www.un.org/Depts/DGACM/index_french.htm

public enum SomeApiConstants {;
  // private constant 
  private static final String PREFIX = "/user";
  private static final String DT_FORMAT = "yyyyMMdd HH:mm:ss";

  // public constants
  public static final String SOME_CONST = "SOME_VALUE";
  public static final String STARTED = ofPattern(DT_FORMAT).format(Instant.now());

  //may be in hierarchy (public/private)
  public enum ApiMapping {;
    public static final String VERSION = PREFIX + "/version";
    public static final String VERSION_LIST = PREFIX + "/list/{type}";
  }
}

<>Pros>

  • clean code
  • core Java
  • the private constructor does not need to be defined
  • attempt to instantiate is validated at compile time as java: enum types cannot be instantiated
  • prevents to clone and deserialization
  • cannot be extended (Enum is final)
  • access modifier public/private/(package private) can be used.

仅使用最后一类。

如果你想要补充其他价值,则使用一个抽象的类别。

使用接口没有多大意义,接口应指明合同。 你只是要宣布一些不变的价值观。

或4. 将其归入含有使用最常数的逻辑的类别

......ry, could;

  1. 私人建筑商的一个不利之处是,这种方法永远无法检测。

  2. Enum by the nature concept good to apply in specific domain type, apply it to decentralized constants looks not good enough

Enum的概念是“数字是一组密切相关的项目”。

  1. Extend/implement a constant interface is a bad practice, it is hard to think about requirement to extend a immutable constant instead of referring to it directly.

  2. 如果采用诸如SonarSource等质量工具,则有规则力量开发商放弃固定的接口,这是一纸空文,因为许多项目具有不断的接口,很少见到经常接口上的“扩展”情况。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签