是否可能调用一个参数对象或参数类是子类,而方法本身以超类作为参数的方法?
我试图使用抽象类Problem的具体实现来调用此方法public void setNewProblem(Problem problem);
。不幸的是,我遇到了NoSuchMethodException
异常。
我像这样调用invoke:
Method method = model.getClass().getMethod("set" + propertyName, new Class[] { newValue.getClass() });
method.invoke(model, newValue);
如果我将
newValue.getClass()
更改为 Problem.class
,一切都可以正常工作。有什么想法如何将子类传递给 public void setNewProblem(Problem problem);
?