Angelika Langer Enum< E延伸Enum< E>>解码 [英] Angelika Langer Enum<E extends Enum<E>> decoding

查看:96
本文介绍了Angelika Langer Enum< E延伸Enum< E>>解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照我的上一个问题,我正在阅读 Angelika Dissecting Enum 。除了一个类型只能为其子类型实例化并且子类型继承了一些常用方法之外,我无法理解文章。 b

  • 以这种方式声明的抽象Enum类的含义是什么?它有什么用?

  • 最后一部分的文档描述了三个方面,有人可以更容易地向我解释它们吗?
  • 在代码草图中看到,Enum类正在声明 compareTo 方法。当Enum隐式实现 Comparable接口。为什么需要定义自己的 compareTo 方法?

  • 看起来像是一个递归泛型的概念。递归泛型究竟意味着什么?在做了一些R& D并理解了我最后一个问题的答案之后,我了解到它迫使这个类自身进行参数化。

  • 然而,详细的解释是有用的。

    解决方案

    我认为将泛型类型声明为类型< E extends Type< E>> 是这样的泛型类将使子类继承返回或接受具有子类型类型的参数的方法。 java.lang.Enum 中的这些方法是:

      public final int compareTo(E o){...} 
    public final Class< E> getDeclaringClass(){...}

    所以,如果我们声明枚举 Color ,隐含的意思是:

      public class Color extends Enum< Color> 

    所以在这个 Enum 的实例中,类型参数 E 被分配了类型参数 Color ,所以上面的方法看起来像这样:

      public final int compareTo(Color o){...} 
    public final Class< Color> getDeclaringClass(){...}


    As per my Previous Question, I am reading the article from Angelika Dissecting Enum. Except for the points that a type can only be instantiated for its subtypes and the subtypes do inherit some common methods, I am not able to understand the article.

    1. What is the meaning of abstract Enum class declared in this way? How is it helpful?
    2. The document in the last part has described three aspects, can someone explain them in easier terms to me?
    3. I do see in the code sketch the Enum class is declaring the compareTo method. When Enum is implicitly implementing Comparable interface. Why do it needs to define its own compareTo method?
    4. Seems like it is a concept of recursive generics. What does recursive generics exactly mean? After doing a bit of R&D and understanding my last question answer, I understand that it forces the class to be parameterized on itself.

    Still, a detailed explanation would be useful.

    解决方案

    I think the main benefit of declaring generic types as Type<E extends Type<E>> is that such generic classes will make subclasses to inherit methods which return or accept arguments with subtype's type. Such methods in java.lang.Enum are:

    public final int compareTo( E o) { ... }
    public final Class< E > getDeclaringClass() { ... }
    

    So, if we declare the enum Color, that implicitly means:

    public class Color extends Enum<Color>
    

    so in this instantiation of Enum the type paramater E is assigned the type argument Color, so the above methods will look like these:

    public final int compareTo(Color o) { ... }
    public final Class<Color> getDeclaringClass() { ... }
    

    这篇关于Angelika Langer Enum&lt; E延伸Enum&lt; E&gt;&gt;解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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