0

我试图找到<init>方法。我有以下课程:

public class Node<T>{ }
public class Main{
    public static void main(String[] args){
        for(Method m: new Node().getClass().getMethods()){
            System.out.println(m.toString());
        }
    }
}

输出:

public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

此类具有默认构造函数。因此,必须将此构造函数转换为将编译的方法<init>的默认构造函数。Node为什么我在运行时看不到他?

4

1 回答 1

3

您需要获取Constructor实例

for (Constructor constructor : new Test().getClass().getConstructors()) {
    System.out.println(constructor);
}

印刷

public test.Test() // In this case, com.yourpackage.Node() for your class
于 2013-10-07T15:35:06.247 回答