ここにいます私の状況の代わりに静的フィールドを使用していますジェネリック<T extends A>クラスはTの1つの
public abstract class Actions {
public static Actions STAND;
public static Actions ATTACK;
public static Actions COLONIZE;
public static Actions DEFEND;
public static Actions TURN_CW;
public static Actions TURN_CCW;
public static Actions DIE;
public abstract long[] getFramesDurations();
public abstract int[] getBaseTiles();
}
public class SimpleActions extends Actions{
public static Actions STAND = new SimpleActions(new long[]{120,120,120,120,120,120,120}, new int[]{0,1,2,3,4,5,6});
public static Actions ATTACK = new SimpleActions(new long[]{120,120,120,120,120,120,120,120,120}, new int[]{7,8,9,10,11,12,13,14,15});
public static Actions COLONIZE = new SimpleActions(new long[]{120,120,120,120,120,120,120}, new int[]{7,8,9,10,11,12,13,14,15});
public static Actions DEFEND = new SimpleActions(new long[]{1}, new int[]{1});
public static Actions TURN_CW = new SimpleActions(new long[]{1}, new int[]{1});
public static Actions TURN_CCW = new SimpleActions(new long[]{1}, new int[]{1});
public static Actions DIE = new SimpleActions(new long[]{1}, new int[]{1});
private final long[] mActionFramesDurations;
private final int[] mActionBaseTiles;
SimpleActions(long[] pActionFramesDurations, int[] pActionBaseTiles) {
mActionFramesDurations = pActionFramesDurations;
mActionBaseTiles = pActionBaseTiles;
}
public long[] getFramesDurations()
{
return mActionFramesDurations;
}
public int[] getBaseTiles()
{
return mActionBaseTiles;
}
}
public abstract class A<T extends Actions> {
A() {
doSomething(T.STAND);
}
protected void doSomething(Actions action) { use action somewhere}
}
public class B extends A<SimpleActions> {
B() {
super();
}
}
アクションがnullであるため、AのコンストラクタはdoSomethingのを呼び出したときに、私は常にNullPointerExceptionが取得..
をBが私を拡張するので、 Actions.STANDではなくSimpleActions.STANDを使用することを期待していました。
私は間違っていますか?私はどのようにこれを行う必要がありますか?
スーパークラスのレベルで 'super()'をクラス 'B'から呼び出すと、コンパイラは' T'が 'SimpleActions'ではなく' Actions'を拡張していることを知っています。あなたのアクションに 'Enum'を使うことを考えましたか、それらを"上書き "する必要がありますか?存在する場合、JavaはSmalltalkから期待されるような静的継承を行いません。あなたのコードがそうであるように、あなたの 'Actions'クラスの静的宣言はまったく無意味です。 –