Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 抽象类的困难:从接口继承_Java_Oop_Inheritance - Fatal编程技术网

Java 抽象类的困难:从接口继承

Java 抽象类的困难:从接口继承,java,oop,inheritance,Java,Oop,Inheritance,请原谅,因为接口对我来说仍然是一个新概念。我试图创建一个简单的重新主题的“乒乓”风格的游戏。我现在正在进行初始设置,我只是在屏幕上创建单独的块,稍后我将在另一个类中操作这些块 我编写了我想用于这个类的所有构造函数、getMethods、setMethods和接口。然而,当我试图编译和运行该类及其运行程序时,我从IDE中得到一个错误,该错误表示“块不是抽象的,并且不会覆盖Locatable中的抽象方法getY() getY()位于接口Locatable中 我尝试将这个类抽象化,这就解决了这个类的问

请原谅,因为接口对我来说仍然是一个新概念。我试图创建一个简单的重新主题的“乒乓”风格的游戏。我现在正在进行初始设置,我只是在屏幕上创建单独的块,稍后我将在另一个类中操作这些块

我编写了我想用于这个类的所有构造函数、getMethods、setMethods和接口。然而,当我试图编译和运行该类及其运行程序时,我从IDE中得到一个错误,该错误表示“块不是抽象的,并且不会覆盖Locatable中的抽象方法getY()

getY()位于接口Locatable中

我尝试将这个类抽象化,这就解决了这个类的问题。但是,在我的runner类中,我无法将对象从runner发送到原始类,因为现在它试图发送到的类是抽象的

这是我遇到问题的课程的开始:

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();   }
这是我的runner课程,用来测试这是否有效:

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);
}   }

在接口或我的“块”类中还有什么我需要的吗?

让我们看看你的类块代码。

你在类中使用了“setYPos”,但在接口中使用了“setY”。您的X和Y getter和setter也有类似的问题。

因为您在Block类中实现了Locatable接口。
根据合同,Block需要为接口中定义的所有方法提供实现。

抽象类无法实例化,因此您必须在Block中实现所有接口方法,或者创建从Block扩展的另一个类并实现抽象方法,或者实现块内联的抽象方法,下面是最后一个选项的示例

Block one = new Block(){
    @override
    public int getY(){
        //do stuff
    }
};

刚刚添加了“Block”代码,让您了解Block类必须从interface@MartinS你能给我一个实现接口的通用语法的例子吗?在整个接口方面仍然是新的。
接口I{public void f();}
然后实现类必须实现函数
类C实现I{@Overrides public void f(){…}