为什么这不是静态的?

时间:2017-01-06 14:26:56

标签: java static subclass

package com.evansgame.newproject.fps;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;
abstract class Weapon{
  int weaponID;//why can't this be static?
  DrawMe drawMe;//why can't this be static?
  int itemID;
  float x;
  float y;
  float z;
  static ReentrantLock weaponsLock = new ReentrantLock();
  static LinkedList<Weapon> weapons = new LinkedList<>();
  boolean active = true;
  Weapon(int itemID, float x, float y, float z){
    this.itemID = itemID;
    this.x = x;
    this.y = y;
    this.z = z;
  }
  static class Bazoooka extends Weapon{
    static final int WEAPON_ID = 0;
    static final DrawMe bazoookaDrawMe = DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f);
    Bazoooka(int itemID, float x, float y, float z){
      super(itemID,x,y,z);
      drawMe = bazoookaDrawMe;//same across all Bazoookas
      weaponID = 0;//same across all Bazoookas
    }
  }
}

变量weaponID和drawMe在所有Bazoooka实例中都是相同的。当我访问武器的实例时,我需要武器ID和DrawMe,无论它碰巧是什么类型的武器。感觉这些变量是静态的,为什么我必须为它们使用实例变量?

3 个答案:

答案 0 :(得分:1)

您可以使用getter而不仅仅是字段:

abstract class Weapon {
    abstract int getID();
    abstract DrawMe getDrawMe();
    ...
}

然后在Bazooka课程中,您只需覆盖以下方法:

static final int WEAPON_ID = 0;
static final DrawMe bazoookaDrawMe = DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f);

@Override
int getID() {
    return WEAPON_ID;
}

@Override
DrawMe getDrawMe() {
    return bazoookaDrawMe;
}

答案 1 :(得分:0)

一个选项:为抽象类提供一个abstract public int getId();方法声明。让每个子类型覆盖它返回自己的静态字段结果。但静态字段应该在子类中声明,而不是在抽象父级中声明。

public abstract class Weapon {
    public abstract int getId();
    public abstract DrawMe getDrawMe();    

    // .... other fields
}

public class Bazooka extends Weapon {

    private static final int BAZOOKA_ID = 200;
    private DrawMe drawMe;

    @Override
    public int getId() {
        return BAZOOKA_ID;
    }

    @Override
    public DrawMe getDrawMe() {
        if (drawMe == null) {
            drawMe = new BazookaDrawMe();
        }
        return drawMe;
    }

}

答案 2 :(得分:0)

我最终使用了枚举

package com.evansgame.newproject.fps;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;
class Weapon{
  static enum WeaponEnum{
    NO_WEAPON(0, new DrawMe(),Player.loadAnimation("standing"),Player.loadAnimation("run"))
    ,BAZOOOKA(1, DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f),Player.loadAnimation("standWithBazoooka"),Player.loadAnimation("runWithBazoooka"));
    final int WEAPON_ID;
    final DrawMe DRAW_ME;
    final Moment[] STANDING_ANIMATION;
    final Moment[] RUNNING_ANIMATION;
    WeaponEnum(int id, DrawMe d, Moment[] standingAnimation, Moment[] runningAnimation){
      WEAPON_ID = id;
      DRAW_ME = d;
      STANDING_ANIMATION = standingAnimation;
      RUNNING_ANIMATION = runningAnimation;
    }
  }
  static ReentrantLock weaponsLock = new ReentrantLock();
  static LinkedList<Weapon> weapons = new LinkedList<>();
  WeaponEnum weaponType;
  int itemID;
  float x;
  float y;
  float z;
  boolean active = true;
  Weapon(WeaponEnum weaponType, int itemID, float x, float y, float z){
    this.weaponType = weaponType;
    this.itemID = itemID;
    this.x = x;
    this.y = y;
    this.z = z;
  }
}