私が持っているとします:
public class FightingZone<MobileSuitso, Background> {
private MobileSuitCollection<MobileSuitso> msCollection;
private BackgroundInfo<Background> bgInfo;
public FightingZone(MobileSuitCollection<MobileSuitso> newCollection, BackgroundInfo<Background> newInfo) {
this.msCollection = newCollection;
this.bgInfo = newInfo;
}
...
...// inside a method
MobileSuitCollection temporaryCollection = new MobileSuitCollection<MobileSuitso>(); // /!\
}
問題は、MobileSuitCollection がインターフェイスであるため、インスタンス化できないことです。たとえば、次のことができます。
MobileSuitCollection temporaryCollection = new GundamMeisterCollection<MobileSuitso>();
MobileSuitCollection temporaryCollection = new InnovatorCollection<MobileSuitso>();
MobileSuitCollection temporaryCollection = new CannonFolderCollection<MobileSuitso>();
など。ただし、を操作するtemporaryCollection
には、パラメーターを介してクラスに渡されたものと同じタイプである必要があります。だから私はこれを行うことを考えました:
if (msCollection instanceof GundamMeisterCollection) {
...
} else if (msCollection instanceof InnovatorCollection) {
...
} ...
しかし、それはひどいことだと思います。これを行うより良い方法はありますか?初期型で使用されるクラスへの参照を保持し、それをインスタンス化することは可能temporaryCollection
でしょうか?