简体   繁体   English

如何覆盖方法但仍然使用 Java 中的变量?

[英]How to override a method but still use variables in Java?

I am quite new to inheritance and all that fun stuff.我对继承和所有有趣的东西都很陌生。 So I have an enemy class that breaks out into a bunch of different types of enemies.所以我有一个敌人类,它会分成一堆不同类型的敌人。 This last enemy I am doing will have a different tick() then the rest so I need to override.我正在做的最后一个敌人将有一个不同的滴答()然后其余的所以我需要覆盖。 However I dont know how to override because then it can't use all the variables that are in the enemy class.但是我不知道如何覆盖,因为它不能使用敌人类中的所有变量。 The option it gives me is to make my get methods static in my GameObject abstract class -> Enemy -> Smart Enemy.它给我的选项是让我的 get 方法在我的 GameObject 抽象类 -> Enemy -> Smart Enemy 中是静态的。 But I am not new to this and I feel like that could cause issues?但我对此并不陌生,我觉得这可能会导致问题?

package first.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;


public class Enemy extends GameObject{

    private Handler handler;
    private Color c; 
    private int eWidth, eHeight, speedX, speedY, x, y;

    public Enemy(int x, int y, int eWidth, int eHeight, int speedX, int speedY, Color c, ID id, Handler handler)
    {
        super(x, y, id);

        this.x = x;
        this.y = y;
        this.id = ID.Enemy;
        this.c = c;
        this.eWidth = eWidth;
        this.eHeight = eHeight;
        this.handler = handler;
        this.speedX = speedX;
        this.speedY = speedY;
    }

    public Rectangle getBounds()
    {
        return new Rectangle(x, y, eWidth, eHeight);
    }

    public void tick()
    {
        x += speedX;
        y += speedY;

        if(y <= 0 || y >= Game.HEIGHT - 48) speedY *= -1; //What can I do instead of 48?
        if(x <= 0 || x >= Game.WIDTH - 32) speedX *= -1;

        handler.addObject(new Trail(x, y, ID.Trail, c, eWidth, eHeight, 0.03f, handler));
    }

    public void render(Graphics g)
    {
        g.setColor(c);
        g.fillRect(x, y, eWidth, eHeight);
    }
}
package first.Game;

import java.awt.Color;

public class SmartEnemy extends Enemy{

    private GameObject player;
    //private Handler handler;
    //private int x, y, speedX, speedY, eWidth, eHeight;
    //private Color c;

    public SmartEnemy(int x, int y, Handler handler)
    {
        super(x, y, 16, 16, 0, 0, Color.green, ID.Enemy, handler);
        //this.handler = handler;
        //this.x = x;
        //this.y = y;

        for(int i = 0; i < handler.object.size(); i++)
        {
            if(handler.object.get(i).getId() == ID.Player)
                player = handler.object.get(i);
        }
    }

    @Override
    public void tick()
    {
        float diffX = x - player.getX();  //- 8
        float diffY = y - player.getY();  //- 8
        float distance = (float) Math.sqrt((x - player.getX()) * (x - player.getX()) + (y - player.getY()) * (y - player.getY()));

        speedX = (int) ((-1.0 / distance) * diffX);
        speedY = (int) ((-1.0 / distance) * diffY);


        if(y <= 0 || y >= Game.HEIGHT - 48) speedY *= -1; //What can I do instead of 48?
        if(x <= 0 || x >= Game.WIDTH - 32) speedX *= -1;

        handler.addObject(new Trail(x, y, ID.Trail, c, eWidth, eHeight, 0.03f, handler));
    }
}

If you wish to access instance variables of Enemy from SmartEnemy , you have several options:如果你想从SmartEnemy访问Enemy实例变量,你有几个选择:

  1. If you only need read only access, add to Enemy class a getter method that returns the value of the required Enemy instance variable.如果您只需要只读访问权限,请向Enemy类添加一个 getter 方法,该方法返回所需的Enemy实例变量的值。 If only classes in the Enemy hierarchy should have access to the value of that instance variable, give it a protected access modifier.如果只有Enemy层次结构中的类可以访问该实例变量的值,请为其提供protected访问修饰符。

  2. If you also need write access, either add a setter method with protected access modifier, or give the instance variable itself a protected access modifier.如果您还需要写访问权限,请添加带有protected访问修饰符的 setter 方法,或者为实例变量本身提供一个protected访问修饰符。

You need some help from protected你需要一些protected帮助

public class Enemy extends GameObject{ 

protected Handler handler; 
protected Color c; 
protected int eWidth, eHeight, speedX, speedY, x, y; 

}

You can access Enemy variables from children classes now.您现在可以从子类访问 Enemy 变量。

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

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