是否有可能弃用java枚举的某些值,如果是,如何?

时间:2022-12-30 21:38:19

I want to deprecate some, but not all possible enumeration values.

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

3 个解决方案

#1


50  

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

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

enum Status {
    OK,
    ERROR,

    @Deprecated
    PROBLEM
}

You can also add a JavaDoc @deprecated tag to document it:

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

enum Status {
    OK,
    ERROR,

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

#2


44  

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

#3


4  

Just tried it eclipse, it works (didn't you try it?):

刚试了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;
    }

}

#1


50  

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

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

enum Status {
    OK,
    ERROR,

    @Deprecated
    PROBLEM
}

You can also add a JavaDoc @deprecated tag to document it:

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

enum Status {
    OK,
    ERROR,

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

#2


44  

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

#3


4  

Just tried it eclipse, it works (didn't you try it?):

刚试了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;
    }

}