简体   繁体   English

使用通用Java类时的InstantiationException

[英]InstantiationException when using a generic java class

I would like to create a simulation containing an entity. 我想创建一个包含实体的模拟。 There can be two kinds of entities, complex and simple ones. 实体可以有两种,复杂的和简单的。 When I instantiate a simple simulation, I want the simple entity to be instantiated, and when I instantiate a complex simulation I want the complex entity to be instantiated. 当我实例化一个简单的仿真时,我希望实例化简单实体,当我实例化一个复杂的仿真时,我希望实例化复杂实体。

Entities: 实体:

class ComplexEntity extends Entity {
    public ComplexEntity(){}
}

class SimpleEntity extends Entity  {
    public SimpleEntity(){}
}

class Entity {
    public Entity(){}
}

Simulations: 模拟:

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();
    }
}

The problem is that when I try to construct a ComplexSimulation: 问题是当我尝试构造ComplexSimulation时:

ComplexSimulation c = new ComplexSimulation();

I get the following InstantiationException: 我得到以下InstantiationException:

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)

The zero argument constructor cannot be the problem because my entities have them... Does anybody know what the problem can be? 零参数构造函数不可能是问题,因为我的实体拥有它们...有人知道问题可能是什么吗?

The problem is that you're using inner classes. 问题是您正在使用内部类。 You cannot create an instance of an inner class without an instance of the outer class, not even if you call Class<InnerClass>.newInstance() . 没有外部类的实例,就不能创建内部类的实例,即使调用Class<InnerClass>.newInstance()

Just make these inner classes to be top classes and your example should work as expected. 只要使这些内部类成为顶级类,您的示例就可以按预期工作。

If you really want/need to initialize non-static inner classes using reflection, see here: How to instantiate inner class with reflection in Java? 如果您确实希望/需要使用反射初始化非静态内部类,请参见此处: 如何在Java中使用反射实例化内部类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-在具有类的泛型类上使用newInstance() <? extends myClass> :InstantiationException - Java - using newInstance() on a Generic Class with Class<? extends myClass>: InstantiationException 在Java中实例化泛型类型时出现InstantiationException - InstantiationException when Instantiate generic type in java 使用Conponent-Scan的抽象类的java.lang.InstantiationException - java.lang.InstantiationException with abstract class Using Conponent-Scan java.lang.InstantiationException尝试实例化子类时出错 - java.lang.InstantiationException Error when try instantiate a children class 在BroadcastReceiver上使用newInstance时的InstantiationException - InstantiationException when using newInstance on BroadcastReceiver Java InstantiationException - Java InstantiationException 使用设备管理员时应用中的Android实例化异常 - Android instantiationexception in app when using device admin 尝试显示标签的权重时使用jGraphT的java.lang.InstantiationException - java.lang.InstantiationException using jGraphT when trying to display the Weight of a lable 从Jar文件加载类:java.lang.InstantiationException - Loading Class from Jar File : java.lang.InstantiationException java.lang.InstantiationException:类没有零参数构造函数错误 - java.lang.InstantiationException: class has no zero argument constructor error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM