Java 类为继承的抽象请求未实现的方法

Java 类为继承的抽象请求未实现的方法,java,methods,abstract,inherited,Java,Methods,Abstract,Inherited,我有这3个类,它们应该一起工作来创建一个游戏。但是我在其中一个地方遇到了一个错误,它希望我添加未实现的方法。创建错误的类称为Game,如下所示 package org.game.main; import java.awt.Graphics2D; public class Game extends Window { public static void main(String[] args) { Window window = new Game(); w

我有这3个类,它们应该一起工作来创建一个游戏。但是我在其中一个地方遇到了一个错误,它希望我添加未实现的方法。创建错误的类称为
Game
,如下所示

package org.game.main;

import java.awt.Graphics2D;

public class Game extends Window {
    public static void main(String[] args) {
        Window window = new Game();
        window.run(1.0 / 60.0);
        System.exit(0);
    }

    public Game() {
        // call game constructor
        super("Test Game", 640, 480);
    }

    public void gameStartup() {

    }

    public void gameUpdate(double delta) {

    }

    public void gameDraw(Graphics2D g) {

    }

    public void gameShutdown() {

    }
}
package org.game.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import org.game.input.*;

/**
 * Game that creates a window and handles input.
 * @author Eric
 */
public abstract class Window extends GameLoop {
    private Frame frame;
    private Canvas canvas;
    private BufferStrategy buffer;
    private Keyboard keyboard;
    private Mouse mouse;
    private MouseWheel mouseWheel;

    /**
     * Creates a new game window.
     *
     * @param title title of the window.
     * @param width width of the window.
     * @param height height of the window.
     */
    public Window(String title, int width, int height) {
        /*Log.debug("Game", "Creating game " +
                title + " (" + width + ", " + height + ")");*/

        // create frame and canvas
        frame = new Frame(title);
        frame.setResizable(false);
        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);
        frame.add(canvas);
        // resize canvas and make the window visible
        canvas.setSize(width, height);
        frame.pack();
        frame.setVisible(true);

        // create buffer strategy
        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

        // create our input classess and add them to the canvas
        keyboard = new Keyboard();
        mouse = new Mouse();
        mouseWheel = new MouseWheel();
        canvas.addKeyListener(keyboard);
        canvas.addMouseListener(mouse);
        canvas.addMouseMotionListener(mouse);
        canvas.addMouseWheelListener(mouseWheel);
        canvas.requestFocus();
    }

    /**
     * Get the width of the window.
     *
     * @return the width of the window.
     */
    public int getWidth()
    {
        return canvas.getWidth();
    }

    /**
     * Get the height of the window.
     *
     * @return the height of the window.
     */
    public int getHeight()
    {
        return canvas.getHeight();
    }

    /**
     * Returns the title of the window.
     *
     * @return the title of the window.
     */
    public String getTitle()
    {
        return frame.getTitle();
    }

    /**
     * Returns the keyboard input manager.
     * @return the keyboard.
     */
    public Keyboard getKeyboard()
    {
        return keyboard;
    }

    /**
     * Returns the mouse input manager.
     * @return the mouse.
     */
    public Mouse getMouse()
    {
        return mouse;
    }

    /**
     * Returns the mouse wheel input manager.
     * @return the mouse wheel.
     */
    public MouseWheel getMouseWheel() {
        return mouseWheel;
    }

    /**
     * Calls gameStartup()
     */
    public void startup() {
        gameStartup();
    }

    /**
     * Updates the input classes then calls gameUpdate(double).
     * @param delta time difference between the last two updates.
     */
    public void update(double delta) {
        // call the input updates first
        keyboard.update();
        mouse.update();
        mouseWheel.update();
        // call the abstract update
        gameUpdate(delta);
    }

    /**
     * Calls gameDraw(Graphics2D) using the current Graphics2D.
     */
    public void draw() {
        // get the current graphics object
        Graphics2D g = (Graphics2D)buffer.getDrawGraphics();
        // clear the window
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        // send the graphics object to gameDraw() for our main drawing
        gameDraw(g);
        // show our changes on the canvas
        buffer.show();
        // release the graphics resources
        g.dispose();
    }

    /**
     * Calls gameShutdown()
     */
    public void shutdown() {
        gameShutdown();
    }

    public abstract void gameStartup();
    public abstract void gameUpdate(double delta);
    public abstract void gameDraw(Graphics2D g);
    public abstract void gameShutdown();
}
package org.game.main;

public abstract class GameLoop {
    private boolean runFlag = false;

    /**
     * Begin the game loop
     * @param delta time between logic updates (in seconds)
     */
    public void run (double delta) {
        runFlag = true;

        startup();
        // convert the time to seconds
        double nextTime = (double) System.nanoTime() / 1000000000.0;
        double maxTimeDiff = 0.5;
        int skippedFrames = 1;
        int maxSkippedFrames = 5;
        while (runFlag) {
            // convert the time to seconds
            double currTime = (double) System.nanoTime() / 1000000000.0;
            if ((currTime - nextTime) > maxTimeDiff) nextTime = currTime;
            if (currTime >= nextTime) {
                // assign the time for the next update
                nextTime += delta;
                update();
                if ((currTime < nextTime) || (skippedFrames > maxSkippedFrames)) {
                    draw();
                    skippedFrames = 1;
                }
                else {
                    skippedFrames++;
                }
            } else {
                // calculate the time to sleep
                int sleepTime = (int)(1000.0 * (nextTime - currTime));
                // sanity check
                if (sleepTime > 0) {
                    // sleep until the next update
                    try {
                        Thread.sleep(sleepTime);
                    }
                    catch(InterruptedException e) {
                        // do nothing
                    }
                }
            }
        }
        shutdown();
    }

    public void stop() {
        runFlag = false;
    }

    public abstract void startup();
    public abstract void shutdown();
    public abstract void update();
    public abstract void draw();
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The type Game must implement the inherited abstract method GameLoop.update()

    at org.game.main.Game.update(Game.java:5)
    at org.game.main.GameLoop.run(GameLoop.java:26)
    at org.game.main.Game.main(Game.java:8)
它希望我在
窗口
类中实现名为
Update()
的方法。
窗口
类如下所示

package org.game.main;

import java.awt.Graphics2D;

public class Game extends Window {
    public static void main(String[] args) {
        Window window = new Game();
        window.run(1.0 / 60.0);
        System.exit(0);
    }

    public Game() {
        // call game constructor
        super("Test Game", 640, 480);
    }

    public void gameStartup() {

    }

    public void gameUpdate(double delta) {

    }

    public void gameDraw(Graphics2D g) {

    }

    public void gameShutdown() {

    }
}
package org.game.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import org.game.input.*;

/**
 * Game that creates a window and handles input.
 * @author Eric
 */
public abstract class Window extends GameLoop {
    private Frame frame;
    private Canvas canvas;
    private BufferStrategy buffer;
    private Keyboard keyboard;
    private Mouse mouse;
    private MouseWheel mouseWheel;

    /**
     * Creates a new game window.
     *
     * @param title title of the window.
     * @param width width of the window.
     * @param height height of the window.
     */
    public Window(String title, int width, int height) {
        /*Log.debug("Game", "Creating game " +
                title + " (" + width + ", " + height + ")");*/

        // create frame and canvas
        frame = new Frame(title);
        frame.setResizable(false);
        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);
        frame.add(canvas);
        // resize canvas and make the window visible
        canvas.setSize(width, height);
        frame.pack();
        frame.setVisible(true);

        // create buffer strategy
        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

        // create our input classess and add them to the canvas
        keyboard = new Keyboard();
        mouse = new Mouse();
        mouseWheel = new MouseWheel();
        canvas.addKeyListener(keyboard);
        canvas.addMouseListener(mouse);
        canvas.addMouseMotionListener(mouse);
        canvas.addMouseWheelListener(mouseWheel);
        canvas.requestFocus();
    }

    /**
     * Get the width of the window.
     *
     * @return the width of the window.
     */
    public int getWidth()
    {
        return canvas.getWidth();
    }

    /**
     * Get the height of the window.
     *
     * @return the height of the window.
     */
    public int getHeight()
    {
        return canvas.getHeight();
    }

    /**
     * Returns the title of the window.
     *
     * @return the title of the window.
     */
    public String getTitle()
    {
        return frame.getTitle();
    }

    /**
     * Returns the keyboard input manager.
     * @return the keyboard.
     */
    public Keyboard getKeyboard()
    {
        return keyboard;
    }

    /**
     * Returns the mouse input manager.
     * @return the mouse.
     */
    public Mouse getMouse()
    {
        return mouse;
    }

    /**
     * Returns the mouse wheel input manager.
     * @return the mouse wheel.
     */
    public MouseWheel getMouseWheel() {
        return mouseWheel;
    }

    /**
     * Calls gameStartup()
     */
    public void startup() {
        gameStartup();
    }

    /**
     * Updates the input classes then calls gameUpdate(double).
     * @param delta time difference between the last two updates.
     */
    public void update(double delta) {
        // call the input updates first
        keyboard.update();
        mouse.update();
        mouseWheel.update();
        // call the abstract update
        gameUpdate(delta);
    }

    /**
     * Calls gameDraw(Graphics2D) using the current Graphics2D.
     */
    public void draw() {
        // get the current graphics object
        Graphics2D g = (Graphics2D)buffer.getDrawGraphics();
        // clear the window
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        // send the graphics object to gameDraw() for our main drawing
        gameDraw(g);
        // show our changes on the canvas
        buffer.show();
        // release the graphics resources
        g.dispose();
    }

    /**
     * Calls gameShutdown()
     */
    public void shutdown() {
        gameShutdown();
    }

    public abstract void gameStartup();
    public abstract void gameUpdate(double delta);
    public abstract void gameDraw(Graphics2D g);
    public abstract void gameShutdown();
}
package org.game.main;

public abstract class GameLoop {
    private boolean runFlag = false;

    /**
     * Begin the game loop
     * @param delta time between logic updates (in seconds)
     */
    public void run (double delta) {
        runFlag = true;

        startup();
        // convert the time to seconds
        double nextTime = (double) System.nanoTime() / 1000000000.0;
        double maxTimeDiff = 0.5;
        int skippedFrames = 1;
        int maxSkippedFrames = 5;
        while (runFlag) {
            // convert the time to seconds
            double currTime = (double) System.nanoTime() / 1000000000.0;
            if ((currTime - nextTime) > maxTimeDiff) nextTime = currTime;
            if (currTime >= nextTime) {
                // assign the time for the next update
                nextTime += delta;
                update();
                if ((currTime < nextTime) || (skippedFrames > maxSkippedFrames)) {
                    draw();
                    skippedFrames = 1;
                }
                else {
                    skippedFrames++;
                }
            } else {
                // calculate the time to sleep
                int sleepTime = (int)(1000.0 * (nextTime - currTime));
                // sanity check
                if (sleepTime > 0) {
                    // sleep until the next update
                    try {
                        Thread.sleep(sleepTime);
                    }
                    catch(InterruptedException e) {
                        // do nothing
                    }
                }
            }
        }
        shutdown();
    }

    public void stop() {
        runFlag = false;
    }

    public abstract void startup();
    public abstract void shutdown();
    public abstract void update();
    public abstract void draw();
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The type Game must implement the inherited abstract method GameLoop.update()

    at org.game.main.Game.update(Game.java:5)
    at org.game.main.GameLoop.run(GameLoop.java:26)
    at org.game.main.Game.main(Game.java:8)
最后一个类称为
Gameloop
,如下所示

package org.game.main;

import java.awt.Graphics2D;

public class Game extends Window {
    public static void main(String[] args) {
        Window window = new Game();
        window.run(1.0 / 60.0);
        System.exit(0);
    }

    public Game() {
        // call game constructor
        super("Test Game", 640, 480);
    }

    public void gameStartup() {

    }

    public void gameUpdate(double delta) {

    }

    public void gameDraw(Graphics2D g) {

    }

    public void gameShutdown() {

    }
}
package org.game.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import org.game.input.*;

/**
 * Game that creates a window and handles input.
 * @author Eric
 */
public abstract class Window extends GameLoop {
    private Frame frame;
    private Canvas canvas;
    private BufferStrategy buffer;
    private Keyboard keyboard;
    private Mouse mouse;
    private MouseWheel mouseWheel;

    /**
     * Creates a new game window.
     *
     * @param title title of the window.
     * @param width width of the window.
     * @param height height of the window.
     */
    public Window(String title, int width, int height) {
        /*Log.debug("Game", "Creating game " +
                title + " (" + width + ", " + height + ")");*/

        // create frame and canvas
        frame = new Frame(title);
        frame.setResizable(false);
        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);
        frame.add(canvas);
        // resize canvas and make the window visible
        canvas.setSize(width, height);
        frame.pack();
        frame.setVisible(true);

        // create buffer strategy
        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

        // create our input classess and add them to the canvas
        keyboard = new Keyboard();
        mouse = new Mouse();
        mouseWheel = new MouseWheel();
        canvas.addKeyListener(keyboard);
        canvas.addMouseListener(mouse);
        canvas.addMouseMotionListener(mouse);
        canvas.addMouseWheelListener(mouseWheel);
        canvas.requestFocus();
    }

    /**
     * Get the width of the window.
     *
     * @return the width of the window.
     */
    public int getWidth()
    {
        return canvas.getWidth();
    }

    /**
     * Get the height of the window.
     *
     * @return the height of the window.
     */
    public int getHeight()
    {
        return canvas.getHeight();
    }

    /**
     * Returns the title of the window.
     *
     * @return the title of the window.
     */
    public String getTitle()
    {
        return frame.getTitle();
    }

    /**
     * Returns the keyboard input manager.
     * @return the keyboard.
     */
    public Keyboard getKeyboard()
    {
        return keyboard;
    }

    /**
     * Returns the mouse input manager.
     * @return the mouse.
     */
    public Mouse getMouse()
    {
        return mouse;
    }

    /**
     * Returns the mouse wheel input manager.
     * @return the mouse wheel.
     */
    public MouseWheel getMouseWheel() {
        return mouseWheel;
    }

    /**
     * Calls gameStartup()
     */
    public void startup() {
        gameStartup();
    }

    /**
     * Updates the input classes then calls gameUpdate(double).
     * @param delta time difference between the last two updates.
     */
    public void update(double delta) {
        // call the input updates first
        keyboard.update();
        mouse.update();
        mouseWheel.update();
        // call the abstract update
        gameUpdate(delta);
    }

    /**
     * Calls gameDraw(Graphics2D) using the current Graphics2D.
     */
    public void draw() {
        // get the current graphics object
        Graphics2D g = (Graphics2D)buffer.getDrawGraphics();
        // clear the window
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        // send the graphics object to gameDraw() for our main drawing
        gameDraw(g);
        // show our changes on the canvas
        buffer.show();
        // release the graphics resources
        g.dispose();
    }

    /**
     * Calls gameShutdown()
     */
    public void shutdown() {
        gameShutdown();
    }

    public abstract void gameStartup();
    public abstract void gameUpdate(double delta);
    public abstract void gameDraw(Graphics2D g);
    public abstract void gameShutdown();
}
package org.game.main;

public abstract class GameLoop {
    private boolean runFlag = false;

    /**
     * Begin the game loop
     * @param delta time between logic updates (in seconds)
     */
    public void run (double delta) {
        runFlag = true;

        startup();
        // convert the time to seconds
        double nextTime = (double) System.nanoTime() / 1000000000.0;
        double maxTimeDiff = 0.5;
        int skippedFrames = 1;
        int maxSkippedFrames = 5;
        while (runFlag) {
            // convert the time to seconds
            double currTime = (double) System.nanoTime() / 1000000000.0;
            if ((currTime - nextTime) > maxTimeDiff) nextTime = currTime;
            if (currTime >= nextTime) {
                // assign the time for the next update
                nextTime += delta;
                update();
                if ((currTime < nextTime) || (skippedFrames > maxSkippedFrames)) {
                    draw();
                    skippedFrames = 1;
                }
                else {
                    skippedFrames++;
                }
            } else {
                // calculate the time to sleep
                int sleepTime = (int)(1000.0 * (nextTime - currTime));
                // sanity check
                if (sleepTime > 0) {
                    // sleep until the next update
                    try {
                        Thread.sleep(sleepTime);
                    }
                    catch(InterruptedException e) {
                        // do nothing
                    }
                }
            }
        }
        shutdown();
    }

    public void stop() {
        runFlag = false;
    }

    public abstract void startup();
    public abstract void shutdown();
    public abstract void update();
    public abstract void draw();
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The type Game must implement the inherited abstract method GameLoop.update()

    at org.game.main.Game.update(Game.java:5)
    at org.game.main.GameLoop.run(GameLoop.java:26)
    at org.game.main.Game.main(Game.java:8)

我希望你能帮助我。我对java非常陌生。

更新方法的签名没有覆盖接口,如果这是您想要的

public void update(double delta)
您需要它来匹配接口

public abstract void update();
因此,听起来这个简单的改变应该会有所帮助:

public abstract void update(double delta);

GameLoop
中,您已定义

public abstract void update();

此方法必须在具有相同签名的子类
Window
Game
中实现。

您已经实现了Game Window和GameLoop类的未实现方法

public abstract void startup();
public abstract void shutdown();
public abstract void update();
public abstract void draw();
实现抽象方法。窗口类的抽象方法如下:

public abstract void gameStartup();
public abstract void gameUpdate(double delta);
public abstract void gameDraw(Graphics2D g);
public abstract void gameShutdown();
还实现了GameLoop类的以下方法

public abstract void startup();
public abstract void shutdown();
public abstract void update();
public abstract void draw();

好吧,我是在你建议的帮助下发现的。这是因为我没有在GameLoop中将Update()定义为

public abstract void update(double delta);
代替

public abstract void update();

所以该方法没有在窗口中被调用。所以,它必须在游戏中被调用。谢谢你的帮助,现在一切正常。

我想这不是我想要的。问题是它希望我实现这种方法。我不想实现它,因为我不需要它。update方法应该更新游戏机制,但是我已经有了另一个方法,叫做gameUpdate()。在Window类中,update方法调用gameUpdate()。因此,在Game.class中没有使用更新。所以我试图摆脱它,希望实现它。你的方法需要匹配抽象版本的签名。您需要更改其中一个。您也可以实现空方法