我想創建一個包含實體的模擬。可以有兩種實體,複雜和簡單的實體。當我實例化一個簡單的仿真時,我希望簡單的實體被實例化,並且當我實例化一個複雜的仿真時,我希望實例化複雜的實體。當使用通用Java類時的InstantiationException
實體:
class ComplexEntity extends Entity {
public ComplexEntity(){}
}
class SimpleEntity extends Entity {
public SimpleEntity(){}
}
class Entity {
public Entity(){}
}
模擬:
class ComplexSimulation extends Simulation<ComplexEntity>
{
public ComplexSimulation() throws InstantiationException, IllegalAccessException {
super(ComplexEntity.class);
}
}
class SimpleSimulation extends Simulation<SimpleEntity>
{
public SimpleSimulation() throws InstantiationException, IllegalAccessException
{
super(SimpleEntity.class);
}
}
class Simulation<E extends Entity> {
protected final E entity;
public Simulation(Class<E> class1) throws InstantiationException, IllegalAccessException
{
entity = class1.newInstance();
}
}
的問題是,當我嘗試構建一個ComplexSimulation:
ComplexSimulation c = new ComplexSimulation();
我得到以下Instantiat ionException:
java.lang.InstantiationException: test.Test$ComplexEntity
at java.lang.Class.newInstance(Unknown Source)
at test.Test$Simulation.<init>(Test.java:55)
at test.Test$ComplexSimulation.<init>(Test.java:37)
at test.Test.go(Test.java:12)
at test.Test.main(Test.java:6)
Caused by: java.lang.NoSuchMethodException: test.Test$ComplexEntity.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
零參數的構造函數不能成爲問題,因爲我的實體有他們......有人知道什麼問題可以是?
例外要告訴你什麼是錯的。發佈堆棧跟蹤。 –
我添加了堆棧跟蹤 – marczoid
您正在使用不刪除的泛型類,泛型類型現在不是實際的類型。 – 2015-10-05 16:27:35