Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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_Inheritance_Abstract_Runtimeexception - Fatal编程技术网

Java 无法解释的“非抽象且不重写抽象方法”错误

Java 无法解释的“非抽象且不重写抽象方法”错误,java,inheritance,abstract,runtimeexception,Java,Inheritance,Abstract,Runtimeexception,我无法解释我试图在游戏中犯的这个错误。我对游戏编程相当陌生,我正在YouTube上学习一个教程。就我所能看到的,我做的和视频中完全一样,但我仍然得到了这个错误 错误:不可编译的源代码-tilegame.entities.biods.Player 不是抽象的,并且不重写中的抽象方法 tilegame.entities.Entity位于 tilegame.entities.biotes.bioter.bioter.java:11 实体类: package tilegame.entities; im

我无法解释我试图在游戏中犯的这个错误。我对游戏编程相当陌生,我正在YouTube上学习一个教程。就我所能看到的,我做的和视频中完全一样,但我仍然得到了这个错误

错误:不可编译的源代码-tilegame.entities.biods.Player 不是抽象的,并且不重写中的抽象方法 tilegame.entities.Entity位于 tilegame.entities.biotes.bioter.bioter.java:11

实体类:

package tilegame.entities;

import java.awt.Graphics;
import java.awt.Rectangle;
import tilegame.Game;
import tilegame.Handler;

public abstract class Entity {

    public static final int DEFAULT_HEALTH = 10;
    protected Handler handler;
    protected float x, y;
    protected int width, height;
    protected int health;
    protected boolean active = true;
    protected Rectangle bounds;

    public Entity(Handler handler, float x, float y, int width, int height){
        this.handler = handler;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        health = DEFAULT_HEALTH;

        bounds = new Rectangle(0, 0, width, height);
    }

    public abstract void tick();

    public abstract void render(Graphics g);

    public abstract void die();

    public void hurt(int amt){
        health += amt;
        if(health <= 0){
            active = false;
            die();
        }
    }

    public boolean checkEntityCollisions(float xOffset, float yOffset){
        for(Entity e : handler.getWorld().getEntityManager().getEntity()){
            if(e.equals(this))
                continue;
            if(e.getCollisionBounds(0f, 0f).intersects(getCollisionBounds(xOffset, yOffset)))
                return true;
        }
        return false;
    }

    public Rectangle getCollisionBounds(float xOffset, float yOffset){
        return new Rectangle ((int) (x + bounds.x + xOffset), (int) (y + bounds.y + yOffset), bounds.width, bounds.height);
    }


    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}
球员级别:

package tilegame.entities.creatures;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import tilegame.Game;
import tilegame.Handler;
import tilegame.gfx.Animation;
import tilegame.gfx.Assets;

public class Player extends Creature{

    //Animations
    private Animation animDown, animUp, animLeft, animRight;

    public Player(Handler handler, float x, float y){
        super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, Creature.DEFAULT_CREATURE_HEIGHT);

        bounds.x = 20;
        bounds.y = 24;
        bounds.width = 23;
        bounds.height = 39;

        //Animations
        animDown = new Animation(250, Assets.player_down);
        animUp = new Animation(250, Assets.player_up);
        animLeft = new Animation(250, Assets.player_left);
        animRight = new Animation(250, Assets.player_right);
    }

    @Override
    public void tick() {
        //Animations
        animDown.tick();
        animUp.tick();
        animLeft.tick();
        animRight.tick();

        //Movement
        getInput();
        move();
        handler.getGameCamera().centerOnEntity(this);
    }

    @Override
    public void die() {
        System.out.println("You died");
    }

    private void getInput(){
        xMove = 0;
        yMove = 0;

        if(handler.getKeyManager().up)
            yMove = -speed;
        if(handler.getKeyManager().down)
            yMove = speed;
        if(handler.getKeyManager().left)
            xMove = -speed;
        if(handler.getKeyManager().right)
            xMove = speed;
    }

    @Override
    public void render(Graphics g) {
        g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);

        //Show bounding box

        /*g.setColor(Color.red);
        g.fillRect((int) (x + bounds.x - handler.getGameCamera().getxOffset()), 
                (int) (y + bounds.y - handler.getGameCamera().getyOffset()),
                bounds.width, bounds.height);*/
    }

    private BufferedImage getCurrentAnimationFrame(){
        if(xMove < 0){
            return animLeft.getCurrentFrame();
        }else if(xMove > 0){
            return animRight.getCurrentFrame();
        }else if(yMove < 0){
            return animUp.getCurrentFrame();
        }else
            return animDown.getCurrentFrame();
    }
}
生物种类:

package tilegame.entities.creatures;

import java.awt.Graphics;
import tilegame.Game;
import tilegame.Handler;
import tilegame.entities.Entity;
import tilegame.tiles.Tile;

public abstract class Creature extends Entity{

    public static final float DEFAULT_SPEED = 3.0f;
    public static final int DEFAULT_CREATURE_WIDTH = 64,
                            DEFAULT_CREATURE_HEIGHT = 64;

    protected float speed;
    protected float xMove, yMove;

    public Creature(Handler handler, float x, float y, int width, int height) {
        super(handler, x, y, width, height);
        speed = DEFAULT_SPEED;
        xMove = 0;
        yMove = 0;
    }

    public void move(){
        if(!checkEntityCollisions(xMove, 0f))
            moveX();
        if(!checkEntityCollisions(0f, yMove))
            moveY();  
    }

    public void moveX(){
        if(xMove > 0){//Moving right

            int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;

            if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)){
                x += xMove;
            }else{
                x = tx * Tile.TILEWIDTH - bounds.x - bounds.width - 1;
            }

        }else if(xMove < 0){//Moving left
            int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;

            if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && 
                    !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)){
                x += xMove;
            }else{
                x = tx * Tile.TILEWIDTH + Tile.TILEWIDTH - bounds.x;
            }
        }
    }

    public void moveY(){
        if(yMove < 0){//Moving up
            int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;

            if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
                    !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)){
                y += yMove;
            }else{
                y = ty * Tile.TILEHEIGHT + Tile.TILEHEIGHT - bounds.y;
            }

        }else if(yMove > 0){ //Moving down
            int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;

            if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
                    !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)){
                y += yMove;
            }else{
                y = ty * Tile.TILEHEIGHT - bounds.y - bounds.height - 1;
            }
        }
    }

    protected boolean collisionWithTile(int x, int y){
        return handler.getWorld().getTile(x, y).isSolid();
    }

    // GETTERS AND SETTERS
    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public float getxMove() {
        return xMove;
    }

    public void setxMove(float xMove) {
        this.xMove = xMove;
    }

    public float getyMove() {
        return yMove;
    }

    public void setyMove(float yMove) {
        this.yMove = yMove;
    }
}
我实现了相同的抽象方法;在石头和树木类也有。我不明白为什么我会犯这个错误。有人愿意解释一下吗?我尝试重新编写代码,重新启动NetBeans。

您的代码是正确的,不会引发任何错误:

abstract class Entity {
    public abstract void die();
}

class Player extends Creature{

    @Override
    public void die() {
        System.out.println("You died");
    }
}

abstract class Creature extends Entity{
}

因此,检查您的导入,您可能在其他包中有一个额外的Player类。

Player需要覆盖diemethod@Kelvin而且它不是???@Kelvin,在我看来它是这样的。请参阅代码。托马斯,也许你应该清理并重建这个项目。@ThomasMathieson你能正确设置ctrl-K错误消息的格式,并告诉我们它发生的时间/地点吗?错误出现在tilegame.entities.biotes.bioter.bioter.java:11,但第11行与玩家无关:public static final float DEFAULT\u SPEED=3.0f;。显示的代码不是给出错误的代码!