原始类型。应参数化对泛型类型的引用 [英] Raw type. References to generic types should be parameterized
问题描述
我有一个Cage类:
public class Cage<T extends Animal> {
// the construtor takes in an integer as an explicit parameter
...
}
我试图在另一个类主方法中实例化Cage的对象:
I am trying to instantiate an object of Cage in another class main method:
private Cage cage5 = new Cage(5);
我收到错误:Cage是原始类型。应参数化对泛型类型Cage的引用。我尝试了几个想法,但我对这种棘手的语法感到困惑:o(
I get the error: Cage is a raw type. References to generic type Cage should be parameterized. I tried several ideas, but am stuck about this tricky syntax :o(
推荐答案
Cage< T>
是一个泛型类型,所以你需要指定一个类型参数,就像这样(假设有一个类Dog extends Animal
):
Cage<T>
is a generic type, so you need to specify a type parameter, like so (assuming that there is a class Dog extends Animal
):
private Cage<Dog> cage5 = new Cage<Dog>(5);
您可以使用任何扩展 Animal
的类型(甚至 Animal
本身)。
You can use any type that extends Animal
(or even Animal
itself).
如果省略类型参数,那么在这种情况下你最终得到的就是 Cage< Animal>
。但是,即使这是你想要的,你仍然应该明确说明类型参数。
If you omit the type parameter then what you wind up with in this case is essentially Cage<Animal>
. However, you should still explicitly state the type parameter even if this is what you want.
这篇关于原始类型。应参数化对泛型类型的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!