要求继承抽象的未实现方法的类

时间:2014-10-08 12:05:20

标签: 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();
        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() {

    }
}

它希望我从Update()类实现名为Window的方法。 Window类看起来像这样。

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

最后一个类名为Gameloop,看起来像这样。

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很新。

4 个答案:

答案 0 :(得分:2)

如果符合您的意图,更新方法的签名不会覆盖界面。

public void update(double delta)

您需要它来匹配界面

public abstract void update();

所以听起来这个简单的改变应该会有所帮助:

public abstract void update(double delta);

答案 1 :(得分:0)

GameLoop已定义

public abstract void update();

此方法必须在其中一个子类WindowGame中使用相同的签名实现。

答案 2 :(得分:0)

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

实施抽象方法。 Window类的抽象方法如下:

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

答案 3 :(得分:0)

好的,我发现了你提出的一些帮助。这是因为我没有在GameLoop中将Update()定义为。

public abstract void update(double delta);

Insted of

public abstract void update();

因此该方法未在Window中调用。所以它必须在游戏中调用。感谢您的帮助,现在一切正常。