简体   繁体   English

有什么方法可以强制类在Java中使用public static final字段?

[英]Any way to force classes to have public static final field in Java?

Is there a way to force classes in Java to have public static final field (through interface or abstract class)? 有没有办法强制Java中的类具有公共静态最终字段(通过接口或抽象类)? Or at least just a public field? 或者至少只是一个公共领域?

I need to make sure somehow that a group of classes have 我需要以某种方式确定一组课程

public static final String TYPE = "...";

in them. 在他们中。

No, you can't. 不,你不能。

You can only force them to have a non-static getter method, which would return the appropriate value for each subclass: 您只能强制它们使用非静态getter方法,该方法将为每个子类返回适当的值:

public abstract String getType();

If you need to map each subclass of something to a value, without the need to instantiate it, you can create a public static Map<Class<?>, String> types; 如果需要将某些子类映射到某个值而不需要实例化它,则可以创建一个public static Map<Class<?>, String> types; somewhere, populate it statically with all the classes and their types, and obtain the type by calling TypesHolder.types.get(SomeClass.class) 在某处,用所有类及其类型静态填充它,并通过调用TypesHolder.types.get(SomeClass.class)获取类型

You can define an interface like this: 您可以定义如下界面:

interface X {
   public static final String TYPE = "...";
}

and you can make classes implement that interface which will then have that field with the same value declared in the interface . 并且您可以使类实现该接口,然后该接口将具有在接口中声明的相同值的字段。 Note that this practice is called the Constant interface anti-pattern . 请注意,这种做法称为Constant接口反模式

If you want classes to have different values then you can define a function in the interface like this: 如果您希望类具有不同的值,那么您可以在界面中定义一个函数,如下所示:

interface X {
   public String getType();
}

and implementing classes will have to implement the function which can return different values as needed. 并且实现类必须实现可以根据需要返回不同值的函数。

Note: This works similarly with abstract classes as well. 注意:这与抽象类的工作方式类似。

没有办法让编译器强制执行此操作,但我会考虑创建一个可以检查这个的自定义FindBugsCheckStyle规则。

I don't think it's possible. 我不认为这是可能的。 But you could make an interface with a getType method 但是你可以使用getType方法创建一个接口

Implement an interface in your classes and call a method from that interface, like others have suggested. 在类中实现一个接口并从该接口调用一个方法,就像其他人建议的那样。

If you must absolutely have a static field, you could make an unit-test that will go through the classes and checks with Reflection API that every class has that public static final field. 如果你必须绝对有一个静态字段,你可以进行一个单元测试,它将通过类和Reflection API检查每个类都有那个公共静态最终字段。 Fail the build if that is not the case. 如果不是这样,则构建失败。

Or at least just a public field? 或者至少只是一个公共领域?

That's IMO the usual way to go: In the superclass, require a value in the constructor: 这是IMO常用的方法:在超类中,需要构造函数中的值:

public abstract class MyAbstract {

  private final String type;

  protected MyAbstract(String type) {
    this.type = type;
  }

  public String getType() {
    return type;
  }
}

This way, all implementations must call that super-constructor - and they don't have to implement getType() each. 这样,所有实现都必须调用该超构造函数 - 并且它们不必每个都实现getType()。

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

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