简体   繁体   English

覆盖方法不起作用

[英]Overiding a method wont work

I am trying to make my own engine but I need some help. 我正在尝试制作自己的引擎,但我需要一些帮助。

I am currently doing the level system. 我目前正在做关卡系统。 The level class extends the render class and the level class overides the Render classes render method. level类扩展了render类,并且level类覆盖了Render类的render方法。 Finally the render class is called from the main class but I dont call the level class. 最后,从主类调用了render类,但我没有调用level类。

EDIT: 编辑:

I have removed the static but now cannot call the render method. 我已经删除了静态方法,但现在无法调用render方法。 I know I am very nooby I kind of teach my self. 我知道我很老套,我会自我教导。

package SimpleEngine.Render;

Render Class (This is called) 渲染类(称为)

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import SimpleEngine.Primitives.*;
import static org.lwjgl.opengl.GL11.glClear;

public class Render {

    public void Render() {

    }

}

Level Class (This is not called) I want this Render method to override the Render classes render method but it doesn't work. Level Class(未调用)我希望此Render方法重写Render类的render方法,但是它不起作用。

package SimpleEngine.Level;

    import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
    import static org.lwjgl.opengl.GL11.glClear;
    import SimpleEngine.Render.*;
    import SimpleEngine.Primitives.*;

    public class Level extends Render {

        public void Render() {
            glClear(GL_COLOR_BUFFER_BIT);
            Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
        }

    }

My main method (calls render but cannot anymore) 我的主要方法(调用render但现在不能了)

package SimpleEngine;

import org.lwjgl.LWJGLException;
import SimpleEngine.Level.*;
import SimpleEngine.Logic.*;
import SimpleEngine.Input.*;
import SimpleEngine.Render.*;
import SimpleEngine.Entites.*;
import SimpleEngine.Timer.*;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;

public class simpleEngine {

    public static final int WIDTH = 640;
    public static final int HEIGHT = 480;
    private static boolean isRunning = true;


    public static void main(String[] args) {
        setUpDisplay();
        setUpOpenGL();
        Entity.setUpEntities();
        Timer.setUpTimer();
        while (isRunning) {
            Render.Render();
            Logic.logic(Timer.getDelta());
            Input.input();
            Display.update();
            Display.sync(60);
            if (Display.isCloseRequested()) {
                isRunning = false;
            }
        }
        Display.destroy();
        System.exit(0);
    }

    private static void setUpDisplay() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.setTitle("SimpleEngine");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }
    }

    private static void setUpOpenGL() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }

}

You can't override static methods. 您不能覆盖静态方法。

As per Oracle tutorail 根据Oracle tutorail

If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. 如果子类定义的类方法具有与超类中的类方法相同的签名,则子类中的方法会将其隐藏在超类中。

AND

You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa. 如果尝试将超类中的实例方法更改为子类中的类方法,并且反之亦然,则会出现编译时错误。

Remove static from Render class render() method and change Level class Render method to render() to introduce overriding behavior Renderrender()方法中删除static Render ,并将Level类的Render方法更改为render()以引入重写行为

EDIT: 编辑:

 while (isRunning) {
            new Level().Render(); 

Note: Java naming convention suggests that method name should start with small letter and name should be camelcase. 注意:Java命名约定建议方法名称应以小写字母开头,名称应为驼峰式。

You can't override a static method. 您不能覆盖静态方法。

Also, be careful because Java is a case sensitive language, so Render() is not the same as render() , and could be a completely different method. 另外,请注意,因为Java是区分大小写的语言,所以Render()render()并不相同,并且可能是完全不同的方法。

I would recommend doing something more generic such as having an interface which defines common methods for any state your game could be in which actually looks like the direction you're heading in. This would just combine your Logic and Render classes. 我建议您做一些更通用的事情,例如为您的游戏可能处于的任何状态定义一个通用方法的接口,该状态实际上看起来像您前进的方向。这将结合您的Logic和Render类。 For example: 例如:

public Interface IGameState {

    public void update(int delta);

    public void render();
}

And then you can have a class that implements that interface such as: 然后,您可以拥有一个实现该接口的类,例如:

public class InGameState implements IGameState {

    public InGameState() { }

    // This is similar to your Logic class
    public void update(int delta) {
        // Perform any updates necessary such as handling keyboard input
    }

    public void render() {
        // What you included in your example
        glClear(GL_COLOR_BUFFER_BIT);
        Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
    }

}

And then your main method could look something like this: 然后您的主要方法可能如下所示:

public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static boolean isRunning = true;
private IGameState currentGameState;

public static void main(String[] args) {
    setUpDisplay();
    setUpOpenGL();
    Entity.setUpEntities();
    Timer.setUpTimer();
    // Create a new game state
    currentGameState = new InGameState();
    while (isRunning) {
        // Change to this type of thing
        // The delta would be the time since last frame so you can stay frame rate                       
        // independent
        currentGameState.update(delta);
        currentGameState.render();
        Display.update();
        Display.sync(60);
        if (Display.isCloseRequested()) {
            isRunning = false;
        }
    }
    Display.destroy();
    System.exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM