나는 모두 동일한 Block
클래스에서 상속받은 여러 클래스가 있으며 변수 값에 따라 그 중 하나를 인스턴스화하려고합니다. 여기 목록에서 유형 선택
public enum MapCases {
FLOOR (Floor.class), // These are all subclass of my Block class
WALL (Wall.class),
ROCK (Rock.class);
private Class<?> blockType;
MapCases (Class<?> pointing) {
this.block = pointing;
}
public Class<?> getType() {
return this.blockType;
}
};
, 내가 그들 일부 데이터 제공 인스턴스화하려고 :
private Block[] blocks; // Array of the mother type
...
blocks = new Block[data.length]; // data is an int array
for (int i = 0; i < data.length; i++) {
int choice = data[i];
Class<?> blockType = MapCases.values()[choice].getType();
blocks[i] = new blockType(); // blockType is of unresolved type
}
그러나 이클립스는 해결할 수없는 말을 나에게 오류를 보여줍니다 blockType을 유형 (java가 아직 유형을 알지 못한다는 사실을 감안할 때 논리적으로 보임).
어떻게하면 되겠습니까?
시도해보십시오. 당신이 가지고있는 모든 곳에서 블록을 확장합니다. '?'. –
나는 이런 일들이 가능하다는 것을 몰랐다. 그래도 작동하지 않습니다. 유형이 아직 해결되지 않았습니다. – Malharhak