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
443 views
in Technique[技术] by (71.8m points)

abstract class - The method *****is undefined for the type***** in Java

I'm trying to call a method from an abstract class Sprite in another package, but I got "The method getSymbol() is undefined for the type Sprite"

Here's the code.

And here is the code from another package sprites

I guess the problem is that the method from an abstract class cannot be instantiated.

But I don't know how to fix it.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

This is the problem:

public class ArrayGrid<Sprite> implements Grid<Sprite>

The way you've declared the class, Sprite is the name of a type parameter. You've made this a generic class, and I suspect you didn't mean to. Within that class, Sprite refers to the type parameter, not the type - so you could have an ArrayGrid<String> which implemented Grid<String>... at which point you'd have a string array rather than a sprite array, so it's no wonder that getSymbol() wouldn't work, just as one symptom of the problem.

I suspect you just wanted:

public class ArrayGrid implements Grid<Sprite>

At that point, Sprite really refers to the type. And that means you can avoid the code that wouldn't work around arrays, and instead just write:

this.grid = new Sprite[numRows][numColumns];

Then there's no need to suppress the warnings :)


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

...