Java 中类型***** 的方法 ***** 未定义 [英] The method *****is undefined for the type***** in Java

查看:27
本文介绍了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 指的是类型参数,而不是类型 - 所以你可以有一个实现 GridArrayGrid>...此时您将拥有一个字符串数组而不是一个精灵数组,所以难怪 getSymbol() 不起作用,就像 one问题的症状.

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>

此时,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆