本文介绍了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声明的含义是什么?
发布评论