【问题标题】:Class asking for unimplemented methods for inherited abstract类要求继承抽象的未实现方法
【发布时间】:2014-10-08 12:05:19
【问题描述】:

我有这 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() {

    }
}

它希望我从 Window 类中实现名为 Update() 的方法。 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很陌生。

【问题讨论】:

    标签: java methods abstract inherited


    【解决方案1】:

    更新方法的签名不会覆盖接口,如果这是你想要的。

    public void update(double delta)
    

    你需要它来匹配界面

    public abstract void update();
    

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

    public abstract void update(double delta);
    

    【讨论】:

    • 我认为这不是我想要的。问题是它要我实现那个方法。而且我不想实现它,因为我不需要它。 update 方法应该更新游戏机制,但我已经有另一种方法,称为 gameUpdate()。而在 Window 类中,更新方法调用了 gameUpdate()。所以在 Game.class 中不会使用 Update。所以我试图摆脱它想要实现它。
    • 您的方法需要匹配抽象版本的签名。您需要更改其中之一。
    【解决方案2】:

    GameLoop你已经定义了

    public abstract void update();
    

    此方法必须在具有相同签名的子类WindowGame 之一中实现。

    【讨论】:

      【解决方案3】:

      你已经实现了 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();
      

      【讨论】:

      • 你也可以实现方法为空
      【解决方案4】:

      好的,我在您建议的一些帮助下找到了它。这是因为我没有在 GameLoop 中将 Update() 定义为。

      public abstract void update(double delta);
      

      插入

      public abstract void update();
      

      所以该方法没有在 Window 中被调用。所以必须在 Game 中调用它。感谢您的帮助,现在一切正常。

      【讨论】:

        猜你喜欢
        • 2013-08-25
        • 2013-10-14
        • 1970-01-01
        • 2014-02-17
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多