Java 中的类型***** 未定义方法*****

The method *****is undefined for the type***** in Java

我试图从另一个包中的抽象 class Sprite 调用方法,但我得到了 "The method getSymbol() is undefined for the type Sprite"

这是代码。

这是来自另一个包 sprites 的代码

我想问题是来自抽象 class 的方法无法实例化。

但是我不知道怎么解决。

这是问题所在:

public class ArrayGrid<Sprite> implements Grid<Sprite>

您声明 class 的方式,Sprite 是类型参数的名称。您已将其设为通用 class,我 怀疑 您不是故意的。在 class 中,Sprite 指的是类型参数,而不是类型 - 所以你可以有一个 ArrayGrid<String> 实现了 Grid<String>...一个字符串数组而不是一个精灵数组,所以难怪 getSymbol() 不起作用,就像 一个 问题的症状一样。

我怀疑你只是想要:

public class ArrayGrid implements Grid<Sprite>

到那个时候,Sprite才真正指代了类型。这意味着您可以避免使用无法处理数组的代码,而只需编写:

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

那么就没有必要抑制警告:)