返回Java中的com.badlogic.gdx.utils.Array<>接口。

huangapple go评论87阅读模式
英文:

Returning com.badlogic.gdx.utils.Array<> of interfaces in Java

问题

我尝试声明方法:

public Array&lt;Shape2D&gt; getShapes(){
...
}

然后我收到编译器错误,指出在圆括号前缺少';'。而且在getShapes内部的return语句也被下划线标记,并显示提示“无法从void返回类型的方法返回值”。我不得不说我是Java的新手,但我认为它会工作。我不能返回Array&lt;&gt;类型吗?

英文:

I tried to declare method:

public Array&lt;Shape2D&gt; getShapes(){
...
}

And I got compiler error, that ';' is missing before round brackets. And return statement inside getShapes also is underlined with hint "Cannot return a value from method of void return type". I have to say I'm new in Java, but I assumed it would work. Can't I return Array<> type?

答案1

得分: 1

在Java中,Array.class没有类型参数。
您可以使用Shape2D[],这是一个特定类型的实际数组。
如果您的编译器出现与void返回有关的问题,可能是因为Array&lt;Shape2D&gt;不是声明数组的方式。因此,它会认为它是一个void类型的方法。

在这种情况下,您的代码应该像这样:

public Shape2D[] getShapes(){
...
}

如果Array.class不是下面提到的那个类,则可能导入了其他内容。

提到的类:
java.lang.reflect.Array

英文:

In Java, the Array.class does not have type parameters.
What you can do is Shape2D[] instead which is an actual array of this specific type.
If your compiler sees a problem involving the void return, it's probably because Array&lt;Shape2D&gt; isn't the way you declare an array. Therefore, it thinks it's a void type method.

In this case, your code needs to look like this:

public Shape2D[] getShapes(){
...
}

If the Array.class is not the one mentioned below, then you may have imported something else.

Mention class:
java.lang.reflect.Array

答案2

得分: 0

抱歉,我之前没有看到括号,尝试在另一个方法内声明方法。对不起浪费了您的时间。

英文:

I didn't see the brackets and tried to declare method inside other method. Sorry for wasting your time.

huangapple
  • 本文由 发表于 2020年8月12日 06:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63367253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定