(继承)java文件编译失败

问题描述 投票:0回答:1

我正在使用 apache netbeans IDE。当我编写以下代码时,它提示错误:文件无法编译,但IDE没有指出错误所在..所以我很困惑 代码:

package entity;

import java.awt.Graphics2D;
import main.Game;
import main.GamePanel;


public class Repair extends Loot{
       
    public Repair(GamePanel gp, double x, double y) {
        this.gp = gp;
        width = 20;
        height = 20;
        this.x = x - width/2;
        this.y = y - height/2;
        trackX = 0;
        trackY = 0;
    }
    
    @Override
    public void update() {
        
    }
    @Override
    public void draw(Graphics2D g2) {
        g2.drawImage(image, Game.round(x), Game.round(y), null);
        // DEBUG
        // this.drawHitboxes(g2);
    }
}

相关代码Loot Class:

package entity;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import main.Game;
import main.GamePanel;
import main.UtilityTool;


public class Loot extends Entity{
    // coins
    private int money;
    private BufferedImage coin;
    private final int hitboxNum;
    protected final double canTrackRange = 50;
    protected boolean canTrack = false;
    protected int trackingTime = 0; // in frames
    // trackingTime / trackTime = 1
    // trackTime = (1 + max_tracking_time) * max_tracking_time / 2
    protected final int trackTime = 465;
    protected double trackX, trackY;
    
    protected int eTime = 60*Game.FPS;
    
    protected void checkIfDestroyed() {
        if (frameCnt >= eTime) isDestroyed = true;
    }
    
    public Loot(GamePanel gp, double x, double y) {
        // x, y are center
        this.gp = gp;
        width = 20;
        height = 20;
        this.x = x - width/2;
        this.y = y - height/2;
        trackX = 0;
        trackY = 0;
        // set images
        this.loadImages();
        image = coin;
        
        money = Game.rand.nextInt(100, 200);
        
        hitboxNum = 1;
        while (hitboxes.size() < hitboxNum) hitboxes.add(new double[]{0});
    }
    private void loadImages() {
        UtilityTool UT = new UtilityTool();
        coin = UT.loadScaleImage("/res/loots/coin.png", width, height);
    }
    private void updateHitBoxes() {
        // 1 hitboxe in total
        // circle
        double[] circle = new double[3];
        double radius = 0.5*width;
        circle[0] = x + radius;
        circle[1] = y + radius;
        circle[2] = radius;
        hitboxes.set(0, circle);
    }
    
    // setters
    public void setMoney(int money) {
        this.money = money;
    }
    public void addAcceleration(double[] pair) {
        if (pair.length == 2) {
            xAPF = pair[0];
            yAPF = pair[1];
        }
    }
    public void setCanTrack(boolean bool) {
        canTrack = bool;
    }
    // getters
    public int getMoney() {
        return money;
    }
    public double getCanTrackRange() {
        return canTrackRange;
    }
    public boolean getCanTrack() {
        return canTrack;
    }
    
    protected void move() {
        // convert acc to speed
        if (xAPF != 0) xSPF = xAPF;
        if (yAPF != 0) ySPF = yAPF;
        // reset acc
        xAPF = 0;
        yAPF = 0;
        // check border
        boolean xIsOut = gp.CH.checkOutOfBorderX(x, width, xSPF);
        boolean yIsOut = gp.CH.checkOutOfBorderY(y, height, ySPF);
        // reverse
        if (xIsOut) xSPF = -xSPF;
        if (yIsOut) ySPF = -ySPF;
        if (canTrack) this.trackToPlayer();
        // move
        x += xSPF;
        y += ySPF;
    }
    // set tracking target
    public void setTarget(double x, double y) {
        x -= 0.5 * width;
        y -= 0.5 * height;
        trackX = x;
        trackY = y;
        
    }
    // if player is nearby
    private void trackToPlayer() {
        trackingTime += 1;
        // get should travel this frame
        double ratio = Math.min((double)(trackingTime)/(double)(trackTime), 1);
        double xSpeed = ratio * (trackX - x);
        double ySpeed = ratio * (trackY - y);
        xSPF = xSpeed;
        ySPF = ySpeed;
    }

    @Override
    public void update() {
        // update move
        this.move();
        this.updateHitBoxes();
        this.checkIfDestroyed();
        
        frameCnt += 1;
    }

    @Override
    public void draw(Graphics2D g2) {
        g2.drawImage(image, Game.round(x), Game.round(y), null);
        // DEBUG
        // this.drawHitboxes(g2);
    }
    private void drawHitboxes(Graphics2D g2) {
        UtilityTool UT = new UtilityTool();
        UT.drawHitboxes(hitboxes, g2);
    }
    
}

相关抽象类Entity:

package entity;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import main.GamePanel;

public abstract class Entity {
    // coordinates
    protected double x = 0, y = 0;
    // size
    protected int width = 100, height = 100;
    // health
    protected double health = 100;
    // speed / frame
    protected double xSPF = 0, ySPF = 0;
    // acceleration
    protected double xAPF = 0, yAPF = 0;
    // collision
    protected boolean canCollide = true;
    // states
    protected boolean isDestroyed = false;
    // image
    protected BufferedImage image = null;
    // access to game panel
    protected GamePanel gp = null;
    // elapsed time
    protected int frameCnt = 0;
    // hitboxes
    public final ArrayList<double[]> hitboxes = new ArrayList<>();
    
    // Main Methods
    public abstract void update();
    public abstract void draw(Graphics2D g2);
    // setters
    public void setX(double x) {this.x = x;}
    public void setY(double y) {this.y = y;}
    public void setWidth(int width) {this.width = width;}
    public void setHeight(int height) {this.height = height;}
    public void setHealth(double health) {this.health = health;}
    public void setXSPF(double xSPF) {this.xSPF = xSPF;}
    public void setYSPF(double ySPF) {this.ySPF = ySPF;}
    public void setXAPF(double xAPF) {this.xAPF = xAPF;}
    public void setYAPF(double yAPF) {this.yAPF = yAPF;}
    public void setCanCollide(boolean canCollide) {this.canCollide = canCollide;}
    public void setIsDestroyed(boolean isDestroyed) {this.isDestroyed = isDestroyed;}
    // getters
    public double getX() {return x;}
    public double getY() {return y;}
    public int getWidth() {return width;}
    public int getHeight() {return height;}
    public double getHealth() {return health;}
    public double getXSPF() {return xSPF;}
    public double getYSPF() {return ySPF;}
    public double getXAPF() {return xAPF;}
    public double getYAPF() {return yAPF;}
    public boolean getCanCollide() {  return canCollide;}
    public boolean getIsDestroyed() {  return isDestroyed;}
    public int getFrameCnt() {return frameCnt;}

}

我真的不知道出了什么问题。 :(

我研究了Java继承,这种类型的单继承确实是允许的。我尝试创建修复对象,下面是错误:

Exception in thread "Thread-0" java.lang.RuntimeException: Uncompilable code - constructor Loot in class entity.Loot cannot be applied to given types;
  required: main.GamePanel,double,double
  found:    no arguments
  reason: actual and formal argument lists differ in length
    at entity.Repair.<init>(Repair.java:1)
    at entity.Enemy.spawnLoot(Enemy.java:144)
    at entity.Enemy.update(Enemy.java:156)
java inheritance
1个回答
0
投票

我认为你缺少超类的构造函数调用。

public Repair(GamePanel gp, double x, double y) {
        this.gp = gp;
        this.x = x - width/2;
        this.y = y - height/2; // call the Loot Constructor (gp,x,y)
       
    }

您应该调用 Loot 构造函数并为其提供所需的参数。

© www.soinside.com 2019 - 2024. All rights reserved.