标签: getconstructor

getConstructor()在java中没有参数

我似乎无法getConstructor用于没有参数的构造函数.我一直在

java.lang.NoSuchMethodException: classname.<init>()
Run Code Online (Sandbox Code Playgroud)

这是代码

interface InfoInterface {
    String getClassName();
    String getMethodName();
    String getArgument();
}

class asa implements InfoInterface {
    @Override
    public String getClassName() {
        return ("jeden");
    }
    @Override
    public String getMethodName() {
        return ("metoda");
    }
    @Override
    public String getArgument() {
        return ("krzyk");
    }
}

class Jeden {
    Jeden() {
        System.out.println("konstruktor");
    }

    public void Metoda(String s) {
        System.out.println(s);
    }
}

class Start {
    public static void main(String[] argv) {
        if (argv.length == 0) {
            System.err.println("Uzycie programu: java Start nazwa_klasy …
Run Code Online (Sandbox Code Playgroud)

java getconstructor

11
推荐指数
1
解决办法
2万
查看次数

Java Reflection getConstructor方法

让我们说我有课程,AB在哪里B扩展A.我也有一个B带有一个类型参数的构造函数A.我也有一个B叫做类型的对象bObj.

有没有办法调用B.class.getConstructor(new Class[] { bObj.getClass() })并获得构造函数,因为B扩展A?目前我正在得到一个NoSuchMethodException.

此致,斯坦

java reflection inheritance extends getconstructor

7
推荐指数
1
解决办法
4020
查看次数

Java - getConstructor()?

我把这个问题写成代码中的注释,我觉得这样比较容易理解.

public class Xpto{
    protected AbstractClass x;

    public void foo(){

       // AbstractClass y = new ????? Car or Person ?????

       /* here I need a new object of this.x's type (which could be Car or Person)
          I know that with x.getClass() I get the x's Class (which will be Car or 
          Person), however Im wondering how can I get and USE it's contructor */

       // ... more operations (which depend on y's type)
    }

}

public abstract class AbstractClass { …
Run Code Online (Sandbox Code Playgroud)

java reflection getconstructor

4
推荐指数
1
解决办法
6087
查看次数

如果使用getConstructor()的内部类,如何创建实例

可能重复:
Java:如何加载已经在类路径上的类(及其内部类)?

有人可以帮我理解如何使用getConstructor创建内部类的实例.

这就是我现在所处的位置.

import java.lang.reflect.*;

public class Outer{
public Outer(int i){
//things to do
}
public class Inner{
Class<?>[] type = new Class<?>[1];
Class<?> myClass;
    public Inner(int i){
    //stuff and code
    }

    public void task(){
    type[0] = Integer.class;
    try{
        myClass = Class.forName("Outer$Inner");
        Constructor construct = myClass.getConstructor(type);
        Object i = construct.newInstance(new Integer(43));
    }
    catch(Exception e){
        e.printStackTrace();
    }
    }
}

public static void main(String[] args){
Outer outer = new Outer(new Integer(21));
Inner inner = outer.new Inner(new Integer(22));
inner.task();
}
Run Code Online (Sandbox Code Playgroud)

}

错误信息 …

java reflection nosuchmethoderror getconstructor

3
推荐指数
1
解决办法
6460
查看次数

我不知道如何使用类类型作为参数

所以我正在创建一个插件插件,但这并不是那么重要.

我有一个Selection类,它保存有关x,z,宽度,深度内容的2D区域的信息.在某个点上,矩形或椭圆将填充到该选择中.我的Ellipse和Rectangle类实现了一个接口Shape.

在Selection的consturctor中,我想确定以后使用的形状的类型.我的第一个想法是传递类类型

public Selection(Class<Shape> shapeType) {
Run Code Online (Sandbox Code Playgroud)

所以我之后可以创建该类的对象

Constructor<Shape> cons = shapeType.getConstructor(//Well I also don't know what belongs here);
Shape s = cons.newInstance(this);
Run Code Online (Sandbox Code Playgroud)

最后我得到了一个N​​ullpointerException,因为Shape接口没有构造函数.

我很困惑.我应该将Shape接口更改为抽象类,还是只在构造函数中传递一些像ELLIPSERECTANGLE这样的int ?有人知道我能做什么吗?

在此先感谢Mighty One

java interface getconstructor type-parameter

1
推荐指数
1
解决办法
60
查看次数