[英]getConstructor() return a constructor that is not implemented
我正在學習 Java 中的一些反射功能,並且在使用getConstructor()
測試getConstructor()
函數時遇到了一個奇怪的問題。
public class IntegerSequence {
private Integer[] elements;
private int size;
private int MAX_SIZE = 100;
public IntegerSequence() {
elements = new Integer[MAX_SIZE];
size = 0;
System.out.println("Hello Guys");
}
}
該函數返回一個有效的構造函數,但永遠不會打印“Hello Guys”消息。
此外,如果我刪除IntegerSequence
的構造函數,它也會返回一個有效的構造函數並且不會拋出任何異常,即使IntegerSequence
類中不再有異常。
我讀到getConstructor()
只返回類中編碼的構造函數,而不是由 Java 自動生成的構造函數,所以我有點迷茫。
這是使用該函數的代碼及其輸出:
public void invokeDefaultConstructor(Class c){
Constructor build = null;
try {
build = c.getConstructor();
} catch (NoSuchMethodException e) {
System.out.println(e);
e.printStackTrace();
}
System.out.println(build.toString());
System.out.println(build.getName());
}
控制台輸出:
public generics.IntegerSequence()
generics.IntegerSequence
你知道什么會導致這種行為嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.