是否可以弃用 Java 枚举的某些值,如果可以,如何弃用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7392230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:45:47  来源:igfitidea点击:

Is it possible to deprecate some of the values of a Java enum and if so, how?

javaenumsdeprecated

提问by Steve Cohen

I want to deprecate some, but not all possible enumerationvalues.

我想弃用一些但不是所有可能的枚举值。

回答by Jesper

Yes, put a @Deprecated annotation on them. For example:

是的,在它们上面放一个@Deprecated 注释。例如:

enum Status {
    OK,
    ERROR,

    @Deprecated
    PROBLEM
}

You can also add a JavaDoc @deprecatedtag to document it:

您还可以添加一个 JavaDoc@deprecated标签来记录它:

enum Status {
    OK,
    ERROR,

    /**
     * @deprecated Use ERROR instead.
     */
    @Deprecated
    PROBLEM
}

回答by Barend

public enum Characters {
    STAN,
    KYLE,
    CARTMAN,
    @Deprecated KENNY
}

回答by home

Just tried it eclipse, it works:

刚刚试过eclipse,它的工作原理:

public class Test {

    public static void main(String[] arg) {

        System.err.println(EnumTest.A);
        System.err.println(EnumTest.B);

    }

    public static enum EnumTest {
        A, @Deprecated B, C, D, E;
    }

}