声明变量为Enum< EnumType>之间的差异和EnumType [英] Difference between declare variable as Enum<EnumType> and EnumType

查看:136
本文介绍了声明变量为Enum< EnumType>之间的差异和EnumType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 java.sql.SQLException:针对枚举的行1针对列gender的数据截断

我有这个枚举

  public enum MyEnum {
Value1,
Value2;
}

此示例代码:

  MyEnum raw = MyEnum.Value1; 
枚举< MyEnum> wrapped = MyEnum.Value1;




  1. System.out.println(raw) ;

  2. System.out.println(wrapped);

  3. System.out.println(raw.getDeclaringClass());

  4. System.out.println (wrapped.getDeclaringClass());

  5. System.out.println(Arrays.toString(raw.values()));

  6. System.out.println(wrapped.values());

  7. System.out.println(raw.equals(wrapped));

  8. 系统.out.println(wrapped.equals(raw));

而对于

$点数1-2,3-4 :返回相同结果

  • >点5 :return [Value1,Value2](但应以静态方式进行访问)

  • 点6 :编译错误:未定义类型枚举< MyEnum> 。为了得到它的工作,我已经放置一个演员为 System.out.println(((MyEnum)包装).values());
  • $ b $第7点
    :返回 true
  • Point 8 :返回 true



  • 在成瘾中,使用调试器检查变量共享是相同的对象(相同的对象id)。

    两种类型的声明之间的区别是什么?

    解决方案

    差异很简单:它与

      String foo =bla之间的区别是一样的

      Object foo =bla; 






    枚举< MyEnum> MyEnum 的超类。所以当使用超类时,已经在超类中定义的方法可用,但是在特定子类中定义的方法在没有类型转换的情况下是不可用的。当调用在特定枚举类中被覆盖的实例方法时,子类方法一直与Java一起被调用。



    使用枚举< MyEnum> 不太有意义。使用 E扩展枚举< E> 然而,通用代码使用的构造能够处理任意的枚举。像 EnumMap EnumSet


    I got inspiration from java.sql.SQLException: Data truncated for column 'gender' at row 1 for enum.
    I have this enum

    public enum MyEnum {
      Value1,
      Value2;
    }
    

    and this sample code:

    MyEnum raw = MyEnum.Value1;
    Enum<MyEnum> wrapped = MyEnum.Value1;
    

    1. System.out.println(raw);
    2. System.out.println(wrapped);
    3. System.out.println(raw.getDeclaringClass());
    4. System.out.println(wrapped.getDeclaringClass());
    5. System.out.println(Arrays.toString(raw.values()));
    6. System.out.println(wrapped.values());
    7. System.out.println(raw.equals(wrapped));
    8. System.out.println(wrapped.equals(raw));

    And for

    • Points 1-2, 3-4: return the same result
    • Point 5: return [Value1, Value2] (but should be accessed in a static way to be correct)
    • Point 6: Compile error: undefined for type Enum<MyEnum>. In order to get it works I have place a cast as System.out.println(((MyEnum) wrapped).values());
    • Point 7: Return true
    • Point 8: Return true

    In addiction, checking with a debugger, variables share are the same object (same object id).
    Which are the differences between two types of declaration?

    解决方案

    The difference is quite simple: it’s the same as the difference between

    String foo="bla";
    

    and

    Object foo="bla";
    


    Enum<MyEnum> is the superclass of MyEnum. So when using the superclass, methods already defined in the superclass are available but methods defined in the specific subclass are not available without the type cast. When invoking instance methods which have been overridden in the specific enum class, the subclass method is invoked, as always with Java.

    Using Enum<MyEnum> makes not much sense. Using E extends Enum<E> however is a construct used by Generic code being able to deal with arbitrary enums. Like EnumMap or EnumSet.

    这篇关于声明变量为Enum&lt; EnumType&gt;之间的差异和EnumType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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