Java的Enum声明的含义是什么? [英] What is the meaning of the Java's Enum declaration?

查看:122
本文介绍了Java的Enum声明的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览Java的源代码,我发现了下面的声明:

  public abstract class Enum< E extends Enum< E  - 代替;> 

应该如何解释?我坚持使用它...



谢谢。

解决方案

<这就像奎宁

  public abstract class Foo< E extends Foo< E>> 
{

public static void use(Foo< E> foo){
//使用foo
}
}

如果您有以下课程:

  public class FooImpl extends Foo< FooImpl> {b $ b // ... 
}

然后这些递归的魔力模板给你,是这样的:




  • Foo 模板要求其参数扩展本身(Foo)。
  • 如果参数类 E 继而扩展 Foo< E> / code>(它必须因为前一点),那么你已经确保 Foo 模板具有它的子类的感知,因为它的子类是作为模板参数传递给它

  • 这又意味着 Foo 的方法可以安全地向下转换这个指向其派生子类 E 的指针。


Surfing on the source code of Java, I found the following declaration:

public abstract class Enum<E extends Enum<E>>

How should it be interpreted? I'm stuck with it...

Thank you.

解决方案

It's like quining! @LES2 is on the right track.

public abstract class Foo <E extends Foo<E>> 
{

   public static void use(Foo<E> foo) {
      // use foo
   }
}

If you have the following class:

public class FooImpl extends Foo<FooImpl> {
  // ...
}

then the magic that these recursive templates gives you, is that:

  • the Foo template requires that its parameter extends itself (Foo).
  • If the parameter class E, in turn, extends Foo<E> (it has to because of the previous point) then you have ensured that the Foo template has an "awareness" of its subclass, since its subclass is passed into it as a template parameter
  • which in turn means that Foo's methods can safely downcast the this pointer to its derived subclass E.

这篇关于Java的Enum声明的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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