如果把圖二的私人變數改為共有的,就沒有例外了


uj5u.com熱心網友回復:
獲取構造方法的方法使用getDeclaredConstructor代替, 你可以點getConstructor這個方法去看看,注釋如下
/**
* Returns a {@code Constructor} object that reflects the specified
* public constructor of the class represented by this {@code Class}
* object. ....
*/
只能獲取public 的constructor
// getConstructor只能獲取public的構造方法
// Constructor constructor = c.getConstructor(int.class, String.class, Float.class);
Constructor declaredConstructor = c.getDeclaredConstructor(int.class, String.class, Float.class);
declaredConstructor.setAccessible(true);
Object obj = declaredConstructor.newInstance(10, "心態不好", 5.26f);
System.out.println(obj);
uj5u.com熱心網友回復:
.getConstructor() 只能獲取類中public修飾的構造器方法;.getDeclaredConstructor() 可以獲取類中所有訪問修飾符修飾的構造器方法;
你需要獲取的時private的構造器方法,因此應該使用 c.getDeclaredConstructor(int.class, String.class, Float.class);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/236695.html
標籤:Java相關
上一篇:JPA兩次join的specification怎么寫?
下一篇:dubbo zookeeper注冊報 Will not attempt to authenticate using SASL錯誤
