Suppose I have:
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;
}
...
...
MobileSuitCollection temporaryCollection = new MobileSuitCollection<MobileSuitso>();
}
The problem is that MobileSuitCollection is an interface, so I cannot create it. For example, I could do:
MobileSuitCollection temporaryCollection = new GundamMeisterCollection<MobileSuitso>();
MobileSuitCollection temporaryCollection = new InnovatorCollection<MobileSuitso>();
MobileSuitCollection temporaryCollection = new CannonFolderCollection<MobileSuitso>();
etc .. However, in order to manipulate temporaryCollection, I need it to be of the same type as the one that went through the parameter to my class. So I thought about this:
if (msCollection instanceof GundamMeisterCollection) {
...
} else if (msCollection instanceof InnovatorCollection) {
...
} ...
I understand that this is terrible. Is there a better way to do this? Is it possible to save a reference to the class used by the initial type, and then create an instance temporaryCollectionwith this?
source
share