【发布时间】:2017-12-10 04:55:36
【问题描述】:
今天我决定使用 Java 制作一个基于自上而下的游戏。我已经制作了窗口并包含了 Jframe。但我发现在 Rectagle GetBounds(); 中创建 GameObject 时出现问题; .我不知道到底是什么,因为我是初学者,而且我知道 java 的基础知识:( .
如果有人能帮我解决这个问题,我给出下面的代码示例:
package example;
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class GameObject {
protected int x, y;
protected float velX = 0, velY = 0;
public GameObject(int x, int y) {
this.x = x;
this.y = y;
}
public abstract void tick();
public abstract void render(Graphics g);
public abstract void Rectangle getBounds();
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public float getVelX() {
return velX;
}
public void setVelX(float velX) {
this.velX = velX;
}
public float getVelY() {
return velY;
}
public void setVelY(float velY) {
this.velY = velY;
}
}
代码导致以下错误:
字段 Rectangle 的非法修饰符;只有公共的,受保护的, 允许 private、static、final、transient 和 volatile 返回类型 对于该方法缺少语法错误,插入“;”去完成 FieldDeclaration 此方法需要正文而不是分号 void 是变量 Rectangle 的无效类型
注意:我使用的是 Java SE-8 和 Eclipse Oxigen。
【问题讨论】:
-
如果
getBounds()返回一个Rectangle它不应该有void。 -
void和Rectangle都是getBounds的返回类型。一个方法不能有多种返回类型。 -
public abstract void Rectangle getBounds();应该是public abstract Rectangle getBounds();
标签: java rectangles