简体   繁体   English

覆盖java.lang.Enum.values()上的Javadoc注释

[英]Overriding Javadoc comment on java.lang.Enum.values()

I have a very specific problem about the method java.lang.Enum .values(). 关于方法java.lang.Enum .values(),我有一个非常具体的问题。

I would like to override its javadoc. 我想覆盖它的javadoc。 Very precisely, the current javadoc for this is, after I created my own enum: 非常准确地说,在我创建自己的枚举后,当前的javadoc:

public static MyClass.MyEnum[] values()

  ... 
  This method may be used to iterate over the constants as follows:

    for (MyClass.MyEnum c : MyClass.MyEnum.values())
    System.out.println(c);

  Returns:
    ...

But in my company System.out calls are considered bad practice so I would like it not to be shown. 但在我的公司System.out调用被认为是不好的做法所以我希望它不被显示。 My first try was to override values() but it is apparently not possible. 我的第一次尝试是覆盖values()但显然不可能。 Is there another way I can do this? 还有其他方法可以做到这一点吗? Or is the only possibility to update the generated doc ? 或者是更新生成的doc的唯一可能性?

I am also curious about why values() is not overridable. 我也很好奇为什么values()不能被覆盖。 I read on other questions that "it is generated by the compiler". 我读过其他问题 “它是由编译器生成的”。 But can someone be more precise? 但有人可以更精确吗? It seems that it's generated from the enum's name, but it does not explain why. 它似乎是从enum的名字生成的,但它没有解释原因。

values is a static method and is not subject to overriding. values是一种静态方法,不受覆盖。 You cannot provide your own method to replace the generated one, and this is by specification . 您无法提供自己的方法来替换生成的方法,这是通过规范

There is no standard mechanism to replace the Javadoc of a method whose source code you don't control, but you could probably mess around with either the build tool, or, if all else fails, the final Javadoc HTML. 没有标准的机制来替换你无法控制源代码的方法的Javadoc,但你可能会使用构建工具,或者,如果所有其他方法都失败,那么最终的Javadoc HTML。

我不认为这是可能的,但是如果你愿意的话,你可以提交JDK问题并提供OpenJDK修复。

From the Oracle Java Tutorials : 来自Oracle Java教程

The enum declaration defines a class (called an enum type). 枚举声明定义了一个类(称为枚举类型)。 The enum class body can include methods and other fields. 枚举类主体可以包括方法和其他字段。 The compiler automatically adds some special methods when it creates an enum. 编译器在创建枚举时会自动添加一些特殊方法。 For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. 例如,它们有一个静态值方法,该方法返回一个数组,该数组按照声明的顺序包含枚举的所有值。

So, the method values cannot be overridden, since it's a special method created by the compiler. 因此,方法值不能被覆盖,因为它是编译器创建的特殊方法。 The Eclipse IDE generates this error when you try to do so: 尝试执行此操作时,Eclipse IDE会生成此错误:

The enum (your enum) already defines the method values() implicitly 枚举(您的枚举)已经隐式定义了方法值()

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

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