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
}
}
}
所有Bazoooka实例中的变量robotID和drawMe均应相同。当我访问Weapon的实例时,无论碰巧是哪种类型的武器,我都需要武器ID和DrawMe。感觉这些变量是静态的,为什么我必须为其使用实例变量?
最佳答案
您可以使用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;
}
关于java - 为什么这不能是静态的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41507780/