【发布时间】:2011-06-11 20:44:47
【问题描述】:
我在 Oracle 网络上关注这个article,以便在开发桌面应用程序时实现 MVC。
不过,我有一个问题:我正在使用由SimpleDirectory 和WildcardDirectory 扩展的抽象Directory 类。模型管理器方法之一接受 Directory 作为参数:
public void addDirectoryDummy(Directory d){
System.out.println("Hello!");
}
抽象控制器使用 setModelProperty 调用此方法:
protected void setModelProperty(String propertyName, Object newValue) {
for (AbstractModel model: registeredModels) {
try {
Method method = model.getClass().
getMethod(propertyName, new Class[] {
newValue.getClass()
}
);
method.invoke(model, newValue);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我从我的实际控制器中这样称呼它:
public void dummy( Directory d){
setModelProperty( BACKUP_DUMMY, d );
}
在我看来,我有:
this.controller.dummy( new SimpleDirectory(0,"ciao") );
我有以下错误:
java.lang.NoSuchMethodException: it.univpm.quickbackup.models.BackupManager.addDirectoryDummy(it.univpm.quickbackup.models.SimpleDirectory)
at java.lang.Class.getMethod(Class.java:1605)
我该如何解决这个问题?我在使用 getMethod 时遗漏了一些东西。
编辑:我读过docs 和getMethod 它说
parameterTypes 参数是一个 Class 对象数组,用于标识 方法的形式参数类型, 按照声明的顺序。
所以我猜这就是问题所在。
【问题讨论】:
标签: java reflection inheritance