简体   繁体   English

使用Reflection-with构造函数参数在Abstract Class中创建实例

[英]Create an instance within Abstract Class using Reflection-with constructor parameter

Create an instance within Abstract Class using Reflection I opened similar question before. 使用反射在Abstract Class中创建实例我之前打开过类似的问题。 But now I have changed the Derived class by adding constructor with int parameter. 但是现在我通过添加带有int参数的构造函数来更改了Derived类。 And now I am having and "no such method exception". 现在,我遇到了“没有这种方法异常”的问题。 here is the source code and exception. 这是源代码和异常。

public abstract class Base {

    public Base(){
        this(1);
    }

    public Base(int i){
        super();
    }

    public Base createInstance() throws Exception{
        Class<?> c = this.getClass();       
        Constructor<?> ctor = c.getConstructor();
        return ((Base) ctor.newInstance());     
    }

    public abstract int  getValue();

    }


    public class Derived extends Base{

    public Derived(int i) {
        super(2);
    }

    @Override
    public int getValue() {
        return 10;
    }


    public static void main(String[] args) {
        try {
            Base b1=new Derived(2);
            Base b2 =b1.createInstance();

            System.out.println(b2.getValue());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

It throws this stack trace 它抛出此堆栈跟踪

java.lang.NoSuchMethodException: reflection.Derived.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at reflection.Base.createInstance(Base.java:17)
    at reflection.Derived.main(Derived.java:18)

Please try 请试试

import java.lang.reflect.Constructor;

public abstract class Base {

    public Base() {
        this(1);
    }

    public Base(int i) {
        super();
    }

    public Base createInstance() throws Exception {
        Class<?> c = this.getClass();
        Constructor<?> ctor = c.getConstructor(new Class[] { int.class });
        return ((Base) ctor.newInstance(new Object[] { 1 }));
    }

    public abstract int getValue();

}

As the exception clearly states the problem is that reflection.Derived.<init>() does not exist. 正如异常明确指出的那样,问题在于reflection.Derived.<init>()不存在。 This because your Derived class doesn't have any default constructor. 这是因为您的Derived类没有任何默认构造函数。

You will need to use: 您将需要使用:

Class<?> c = this.getClass();
Constructor<?> ctor = c.getConstructor(Integer.class);
ctor.newInstance(intValue);

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

相关问题 使用Reflection在Abstract Class中创建实例 - Create an instance within Abstract Class using Reflection Java反射创建未知类和构造函数的实例 - Java reflection create instance of unknown class and constructor 寻找Iterable的类以使用反射创建构造函数 - Looking for the class of an Iterable to create a constructor using reflection 是否有可能在java中使用反射创建没有arg构造函数的类的“空白”实例? - Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection? 如何使用零参数构造函数创建具有通过Java反射绑定的上下文的Scala类的新实例? - How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor? 如何创建将接口传递到其构造函数的抽象类的实例? - How to create an instance of an abstract class that passes an interface into its constructor? 使用反射从抽象基类访问构造函数 - Accessing constructor from abstract base class with reflection 隐藏构造函数的抽象类实例 - Instance of abstract class with hidden constructor Java - 使用Reflection创建新实例,而不需要知道构造函数参数 - Java - Create new Instance using Reflection without knowing constructor params 使用Java反射创建静态抽象类的实例? - Creating an instance of a static abstract class with Java reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM