问题描述
我试图理解有界类型,但并不太了解它们的要点.
I am trying to understand bounded types and not quite grasping the point of them.
有一个示例,其中提供此用例:
There is an example of bounded generics on which provides this use case:
public class NaturalNumber<T extends Integer> {
private T n;
public NaturalNumber(T n) { this.n = n; }
public boolean isEven() {
return n.intValue() % 2 == 0;
}
// ...
}
如果要限制可以作为参数化类型的类,为什么不将所有参数化都忘了,并拥有:
If you are going to restrict the classes that can be the parameterized type, why not just forget the parameterization all together and have:
public class NaturalNumber {
private Integer n;
public NaturalNumber(Integer n) { this.n = n; }
public boolean isEven() {
return n.intValue() % 2 == 0;
}
// ...
}
那么扩展/实现Integer
的任何类都可以与此类一起使用.
Then any class that extends/implements Integer
can be used with this class.
还有一个附带的问题:当Java Integer
类为final时,在第一个示例中T
如何扩展Integer
?
Also, a side question: How is T
extending Integer
in the first example when the Java Integer
class is final?
推荐答案
T
只能是Integer
,因此此处的扩展"纯粹是符号性的. (我从旁注开始,因为,实际上,这是一个泛型无用的示例.我真的不知道为什么本教程认为这是一个有用的演示.不是.)
T
can only be Integer
, so the "extends" here is purely symbolic. (I'm starting with the side-note because, indeed, it's an example where generics are useless. I truly have no idea why the tutorial thinks this is an informative demonstration. It's not.)
假设T extends Number
:
class Example<T extends Number> {
private T num;
void setNum(T num) { this.num = num; }
T getNum() { return num; }
}
因此,泛型的要点通常是可以执行以下操作:
So the point of generics in general, is that you can do this:
Example<Integer> e = new Example<>();
e.setNum( Integer.valueOf(10) );
// returning num as Integer
Integer i = e.getNum();
// and this won't compile
e.setNum( Double.valueOf(10.0) );
泛型是参数多态性的一种形式,从本质上讲,它使我们可以重用具有通用性的代码涉及的类型.
Generics are a form of parametric polymorphism, essentially it lets us reuse code with a generality regarding the types involved.
那么界限的意义是什么?
So what's the point of a bound?
这里的绑定意味着T
必须是Number
或Number
的子类,因此我们可以在T
的实例上调用Number
的方法.不幸的是,Number
本身是一个通常没用的基类(由于精度方面的考虑),但是它可能会让我们做一些有趣的事情,例如:
A bound here means that T
must be Number
or a subclass of Number
, so we can call the methods of Number
on an instance of T
. Number
is unfortunately a generally useless base class on its own (because of precision concerns), but it might let us do something interesting like:
class Example<T extends Number> extends Number {
// ^^^^^^^^^^^^^^
...
@Override
public int intValue() {
return num.intValue();
}
// and so on
}
例如,更常见的是找到T extends Comparable<T>
,这使我们可以对T
做一些更有意义的事情.我们可能会有类似的内容:
It's more common, for example, to find T extends Comparable<T>
which lets us do something more meaningful with T
. We might have something like:
// T must be a subclass of Number
// AND implement Comparable
Example<T extends Number & Comparable<T>>
implements Comparable<Example<T>> {
...
@Override
public int compareTo(Example<T> that) {
return this.num.compareTo(that.num);
}
}
现在我们的Example
类具有自然的顺序.即使我们不知道实际上在类体内是什么,我们也可以对其进行排序.
And now our Example
class has a natural ordering. We can sort it, even though we have no idea what T
actually is inside the class body.
如果我们结合这些概念,那就:
If we combine these concepts, that:
- 泛型允许外部世界"指定实际类型,并且
- 界限允许内部世界"使用共同点,
我们可以构建以下结构:
we could build constructs such as:
static <T extends Comparable<T>> T min(T a, T b) {
return (a.compareTo(b) < 0) ? a : b;
}
{
// returns "x"
String s = min("x", "z");
// returns -1
Integer i = min(1, -1);
}
这篇关于了解Java中的有限泛型.有什么意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!