Java的Enum声明的含义是什么?

编程入门 行业动态 更新时间:2024-10-23 15:32:39
本文介绍了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声明的含义是什么?

本文发布于:2023-08-01 09:21:45,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:含义   声明   Java   Enum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!