对于Java中的*****类型,方法*****未定义 [英] The method *****is undefined for the type***** in Java
问题描述
我正在尝试从另一个包中的抽象类Sprite调用一个方法,但我得到了方法getSymbol()未定义Sprite类型
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"
这是代码。
这是来自另一个包精灵的代码
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.
推荐答案
这就是问题所在:
public class ArrayGrid<Sprite> implements Grid<Sprite>
你宣布上课的方式, Sprite
是类型参数的名称。你已经把它变成了一个通用的类,我怀疑你不是故意的。在该类中, Sprite
是指类型参数,而不是类型 - 因此您可以拥有 ArrayGrid< String>
它实现了 Grid< String>
...此时你有一个字符串数组而不是精灵数组,所以毫无疑问 getSymbol ()
不起作用,就像问题的一个症状一样。
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.
我怀疑你只是想要:
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 :)
这篇关于对于Java中的*****类型,方法*****未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!