-2
我在此代碼中遇到了投射問題,但不知道如何更正它。在Java中使用反射時出錯
public void showFrame(String className, Object controller) throws Exception{
try {
Class c = Class.forName("com." + className);
// "(Object.class)" I want this to be of type held in className var
// this type will be same as one passed into "controller" at runtime
Constructor ctr = c.getConstructor(Object.class);
ctr.newInstance(controller);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Error");
}
}
當你決定哪個答案是最有幫助的你,將其標記爲通過點擊複選框大綱答案左側的接受的答案。 http://stackoverflow.com/faq#howtoask – Andrey
請提供一個完整但最小的例子,包括a)一個包含構造函數的示例類b)對該類的'showFrame'的實際調用。 –