有没有办法在 Java 中只允许一种类型作为属性?

Is there a way to allow only one Type as Attribute in Java?

我有一个 class,我只想要这个 class 类型 Pair<Integer, String> 的多个静态属性。不多。有没有办法我可以定义一个 superclass 或其他无法添加另一个变量属性的东西。

重要的是我有多个属性,而不是列表或枚举。

示例:

public class Test {

  public static final Pair<Integer, String> a ...

  public static final Pair<Integer, String> b ...

  public static final Pair<Integer, String> c ...

}

不可能通过创建超级classes 或超级接口来限制任何给定 class 可以声明的静态字段。唯一可以做到的是注释处理器(即使您不使用注释)。但是,没有办法强制执行编译的人 运行 该处理器。

如果您对此感兴趣:这并不容易,解释如何做到这一点将超出 SO 问题的合理范围一个数量级。在网上搜索有关注释处理器的教程。