简体   繁体   English

原始类型。 对泛型类型的引用应该被参数化

[英]Raw type. References to generic types should be parameterized

I have a Cage class:我有一个笼子类:

public class Cage<T extends Animal> {
// the construtor takes in an integer as an explicit parameter
...
}

I am trying to instantiate an object of Cage in another class main method:我正在尝试在另一个类 main 方法中实例化 Cage 的对象:

private Cage cage5 = new Cage(5);

I get the error: Cage is a raw type.我收到错误消息:Cage 是原始类型。 References to generic type Cage should be parameterized.对泛型类型 Cage 的引用应该被参数化。 I tried several ideas, but am stuck about this tricky syntax :o(我尝试了几个想法,但被这个棘手的语法困住了:o(

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 ): Cage<T>是泛型类型,所以你需要指定一个类型参数,像这样(假设有一个class Dog extends Animal ):

private Cage<Dog> cage5 = new Cage<Dog>(5);

You can use any type that extends Animal (or even Animal itself).您可以使用任何扩展Animal类型(甚至是Animal本身)。

If you omit the type parameter then what you wind up with in this case is essentially Cage<Animal> .如果省略 type 参数,那么在这种情况下最终得到的基本上是Cage<Animal> However, you should still explicitly state the type parameter even if this is what you want.但是,即使这是您想要的,您仍然应该明确声明类型参数。

For other java newbie like me.对于像我这样的其他 Java 新手。

  • Code is look like this:代码如下所示:
public class ContinuousAddressBuilder<T> extends VariableLengthPacket {
  ...

  /* T=int/float/double */
  private ArrayList<T> informosomes;

  ...

  public ContinuousAddressBuilder builderCon(int con) {
    ...
  }
}
  • Solution:解决方案:

Add <T> after your class:在你的课后添加<T>

change from从改变

public ContinuousAddressBuilder builderCon(int con)

to

public ContinuousAddressBuilder<T> builderCon(int con)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Type Class is a raw type. References to generic type Class<T> should be parameterized 警告-类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Warning - Class is a raw type. References to generic type Class<T> should be parameterized 类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Class is a raw type. References to generic type Class<T> should be parameterized JComboBox是原始类型。应参数化对泛型类型JComboBox <E>的引用 - JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized 如何解决此警告? -是原始类型。 泛型类型的引用应参数化 - How to address this warning? - Is a raw type. References to generic type should be parameterized AdapterView是原始类型。 泛型类型AdapterView的引用 <T> 应该参数化 - AdapterView is a raw type. References to generic type AdapterView<T> should be parameterized AddLinkEntry类是原始类型。 对泛型类型AddLinkEntry类的引用 <T> 应该参数化 - AddLinkEntry Class is a raw type. References to generic type AddLinkEntry Class<T> should be parameterized 可比是原始类型。 对通用类型Comparable的引用 <T> 应该参数化 - Comparable is a raw type. References to generic type Comparable<T> should be parameterized 枚举是原始类型。 对泛型类型 Enum 的引用<E>应该参数化 - Enum is a raw type. References to generic type Enum<E> should be parameterized 为什么我收到警告:Class是原始类型。对泛型类型<T>的引用应该参数化“? - Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM