且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将String转换为Enum,然后再转换为大写?

更新时间:2023-02-17 20:42:40

不,没有办法.

但是,一旦创建了另一个方法 String getName()来覆盖" name()的默认实现.我认为您必须对 Regular valueOf()做同样的事情,即创建另一个方法 Regular valueOfByName().

But, once you create another method String getName() to "override" the default implementation of name(). I thought that you have to do the same with Regular valueOf() ,i.e., create another method Regular valueOfByName().

public enum Regular {
    NONE,
    HOURLY,
    DAILY,
    WEEKLY;

    public String getName() {
        return this.name().toLowerCase();
    }

    public static Regular valueOfByName(String name){
        return valueOf(name.toUpperCase());
    }
}