から動的アルターサブクラス、:のJava - 次がある私は、クラスを持っているParentclass
final class Attributes {
Attribute agility;
Attribute endurance;
Attribute intelligence;
Attribute intuition;
Attribute luck;
Attribute speed;
Attribute strength;
private ArrayList<Attribute> attributes;
private boolean initialized = false;
public Attributes() throws IllegalAccessException {
initializeAttributes();
store();
}
public ArrayList<Attribute> getAttributes() {
return attributes;
}
private void initializeAttributes() {
if (!initialized) {
agility = new Agility();
endurance = new Endurance();
intelligence = new Intelligence();
intuition = new Intuition();
luck = new Luck();
speed = new Speed();
strength = new Strength();
initialized = true;
}
}
private void store() throws IllegalAccessException {
Field[] fields = this.getClass().getDeclaredFields();
attributes = new ArrayList<Attribute>();
if (initialized) {
for (Field f : fields) {
if (f.getType() == Attribute.class) {
attributes.add((Attribute)f.get(this));
}
}
}
}
私の考えは何とかそうのような方法を使用することです:
public void setAttribute(Class attribute.getClass(), int value) {
}
をこの方法では、呼び出し元は、変更する属性のタイプ(強度、インテリジェンスなど)を指定し、プロセスは、渡された属性サブクラスかどうかを識別することによって、操作する属性を決定するために、上記のベクターのサブクラスと同等であった。この場合、ベクター内のサブクラスが変更されます。
これは可能ですか? obj.getClass()/ getType()== obj2.getClass()/ getType()がサブクラスを考慮に入れているかどうかわからないため、私がこれを求めている主な理由です。
「EnumMap」に言及するために+1 – Nick
私はこれについて考えていましたが、列挙型で静的な値を必要としませんか?私は静的でないものが必要なので、同じ型の複数のインスタンスを持つことができます。そうでなければ、実際には機能しません。 – zeboidlund