枚举和枚举之间的区别 [英] difference between Enum and enum

查看:172
本文介绍了枚举和枚举之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举具有 valueOf(string)方法来获取枚举常量,并且在 java.lang.Enum class name $ code> valueOf(enumClassName,string)
我发现两者都提供相同的输出。那么还有什么其他的区别。如果没有区别,那么为什么JSL添加了 Enum.valueOf()

  enum Season {
WINTER,SUMMER
}

class Test {
public static void main(String [] args){
String season =WINTER ;

// switch(Season.valueOf(colObject))//以下行实现相同的事情
switch(Enum.valueOf(Season.class,season))//两者之间的任何其他区别方法
{
case WINTER:{
System.out.println(在switch case = VENDOR);
break;
}
默认值:
System.out.println(Fell thru。);
break;
}
}
}


解决方案

包含 Enum.valueof()方法的原因是它适用于任何枚举。相比之下,特定方法的枚举 valueof 方法仅适用于特定的枚举 ...因为枚举类不能多态使用。



显然, Enum.valueOf(...)方法只有在实现需要的代码才能处理多个枚举时, code> types ...和泛型不剪切。


enum has valueOf(string) method to get enum constant and the same type of method present in java.lang.Enum class having name valueOf(enumClassName, string) I found both are giving same output. Then what are other differences. If no difference then why JSL added Enum.valueOf()?

enum Season {
    WINTER,SUMMER
}

class Test {
    public static void main(String[] args) {
        String season = "WINTER";

        //switch (Season.valueOf(colObject))  // following line achieves same thing
        switch (Enum.valueOf(Season.class, season)) // any other difference between both approach
        {
            case WINTER: {
                System.out.println("Got it in switch case= VENDOR");
                break;
            }
            default:
                System.out.println("Fell thru.");
                break;
            }
    }
}

解决方案

The reason the Enum.valueof() method is included is that it works with any enum. By contrast, the enum valueof method for a specific method only works for that specific enum ... since enum classes cannot be used polymorphically.

Obviously the Enum.valueOf(...) method is only really useful if you are implementing code that needs to work for multiple enum types ... and generics don't cut it.

这篇关于枚举和枚举之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆