原谅我,因为接口对我来说仍然是一个新概念。我正在尝试创建一个简单的主题为“乒乓球”的游戏。我现在处于初始设置中,在这里我只是在屏幕上创建单个块,稍后将在另一个类中对其进行操作。
我编写了要与此类一起使用的所有构造函数,getMethods,setMethods和Interface。但是,当我尝试编译并运行该类及其运行程序时,我从IDE中收到一条错误消息,提示“块不是抽象的,并且不会在Locatable中覆盖抽象方法getY()”。
getY()在可定位的接口内
我试图使该类抽象化,从而解决了该类的问题。但是,在我的运行器类中,我无法将对象从运行器发送到原始类,因为现在尝试将其发送到的类是抽象的。
这是我遇到问题的课程的开始:
public class Block implements Locatable {
//instance variables
private int xPos;
private int yPos;
private int width;
private int height;
private Color color;
public Block()
{
xPos = 0;
yPos = 0;
width = 0;
height = 0;
}
public Block(int x, int y, int wdth, int ht)
{
xPos = x;
yPos = y;
width = wdth;
height = ht;
}
public Block(int x, int y, int wdth, int ht, Color col)
{
xPos = x;
yPos = y;
width = wdth;
height = ht;
color = col;
}
public void setBlockPos(int x, int y)
{
xPos = x;
yPos = y;
}
public void setXPos(int x)
{
xPos = x;
}
public void setYPos(int y)
{
yPos = y;
}
public void setWidth(int wdth)
{
width = wdth;
}
public void setHeight(int ht)
{
height = ht;
}
public void draw(Graphics window)
{
window.setColor(color);
window.fillRect(getX(), getY(), getWidth(), getHeight());
}
public int getXPos()
{
return xPos;
}
public int getYPos()
{
return yPos;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public String toString()
{
return "" + xPos + " " + yPos + " " + width + " " + height;
}
}
这是我在上面的类中尝试使用的接口:
public interface Locatable {
public void setPos( int x, int y);
public void setX( int x );
public void setY( int y );
public int getX();
public int getY(); }
这是我的跑步者课程,用于测试它是否有效:
class BlockTestOne {
public static void main( String args[] )
{
Block one = new Block();
out.println(one);
Block two = new Block(50,50,30,30);
out.println(two);
Block three = new Block(350,350,15,15,Color.red);
out.println(three);
Block four = new Block(450,50,20,60, Color.green);
out.println(four);
} }
接口或“ Block”类中是否还有其他需要?
您在类中使用了“ setYPos”,但在接口中使用了“ setY”。您的X和Y获取器和设置器也有类似的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句