Java泛型和继承递归 [英] Java Generics and Inheritance recursion
问题描述
我遇到了以下使用泛型和继承的Java代码。我真的不明白下面的代码片段是什么:
class A< B extends A< B>> {
...
}
这段代码做了什么?
(我从 DBMaker在MapDB )
几乎清楚,问题实际上分为两部分:
<1>为什么
B延伸A
? <2>为什么
在 B中扩展A< B>
具有泛型类型 B
?
这些部分的答案是:
<1>特别的例子是这个类( A
)是 builder 类(称为 DBMaker
),所以它的大多数方法返回一些类型,这扩展了这个构建器的类类型。这解释了为什么 B
应该扩展 A
class。
2)但是,实际上,如果我们隐藏第二部分 ... extends A< B>
,我们将只收到 class A< B个
。所以 A
的类型变量类型为 B
。这就是为什么在 ... extends A< B>
中,A被标记为type A
,其类型变量 B
。
I came across the following Java code that uses generics and inheritance. I truly do not understand what the following snippet does:
class A<B extends A<B>> {
...
}
What does this code do?
(I got this from DBMaker in MapDB)
解决方案 It is almost clear and the question actually conists in two parts:
1) why B extends A
?
2) why A
inside B extends A<B>
has generic type B
?
Answers for these parts will be:
1) In particular example this class (A
) is builder class (called DBMaker
), so most of its methods return some type, which extends this builder's class type. This explains, why B
should extend A
class.
2) But, actualy, if we will hide for the second part ...extends A<B>
, we will receive just class A<B>
. So A
has type variable of type B
. That is why in ...extends A<B>
A is marked as type A
having type variable B
.
这篇关于Java泛型和继承递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!