简体   繁体   English

扩展类和静态变量

[英]Extended class and static variable

I've DynamicObject , Player , Enemy classes 我已经DynamicObjectPlayerEnemy

public class DynamicObject {

    protected static float speed;
}



public class Player extends DynamicObject {
       /*some code*/
       Player.speed = 100;
}


public class Enemy extends DynamicObject {
       /*some code*/
       Enemy.speed = 50;
}

And always speed value is overridden. 并始终覆盖speed值。 Of course I can create new speed variable in Player and Enemy , but then the existing DynamicObject class is pointless. 当然我可以在PlayerEnemy创建新的speed变量,但是现有的DynamicObject类是没有意义的。 I want to have different speed values on each class. 我希望每个班级都有不同的speed值。 All objects of current class will have the same speed . 当前类的所有对象将具有相同的speed How I should make it in correct way? 我该如何正确地做到这一点?

The speed variable should not be static . speed变量不应该是static Otherwise it won't be bound to any of the instances of the DynamicObject class, nor any of it's subclasses instances. 否则,它不会绑定到DynamicObject类的任何实例,也不会绑定到任何子类实例。

If you want to have a different speed value for each of the subclasses, you can do: 如果要为每个子类设置不同的speed值,可以执行以下操作:

public class DynamicObject {    
    protected float speed;

    public DynamicObject(float speed) {
        this.speed = speed;
    }

    public float getSpeed() {
       return this.speed;
    }
}

public class Player extends DynamicObject {
   /*some code*/
   public Player(float speed) {
       super(speed);
   }

}


public class Enemy extends DynamicObject {
       /*some code*/
   public Enemy(float speed) {
       super(speed);
   }
}

If every DynamicObject has a speed, and the speed is the same for every instance of, for example, Player, then you should have 如果每个DynamicObject都有一个速度,并且每个实例(如Player)的速度相同,那么你应该拥有

public abstract int getSpeed();

in DynamicObject, and 在DynamicObject中,和

@Override
public int getSpeed() {
    return 100;
}

in Player. 在播放器中。

If you need to have access to the constant speed returned for evry instance of Player without instantiateing a Player, just use 如果您需要访问为播放器的evry实例返回的恒定速度而不实例化播放器,只需使用

public static final int CONSTANT_PLAYER_SPEED = 100;

@Override
public int getSpeed() {
    return 100;
}

and use Player.CONSTANT_PLAYER_SPEED to access the speed of all players. 并使用Player.CONSTANT_PLAYER_SPEED来访问所有玩家的速度。

Remove the static from that variable, if a variable is static, it's belong to the class, not to instances. 从该变量中删除静态,如果变量是静态的,则它属于类,而不属于实例。

And making a static variable protected doesn't make any scene, since, inheritance and static are totally different. 并且保护静态变量不会产生任何场景,因为继承和静态是完全不同的。

protected float speed;

You could also have an abstract method, which would mean that you would always have to override it in your sub classes. 您还可以使用抽象方法,这意味着您必须始终在子类中覆盖它。

public abstract class Dynamic {

protected float speed; 

abstract void setSpeed();
}
public class Player extends Dynamic{
@Override
void setSpeed() {
    this.speed=50;
}

} }

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

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