Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

java - 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:

private Cage cage5 = new Cage(5);

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(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

You can use any type that extends Animal (or even Animal itself).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...