是否可以调用参数对象或参数类为子类且方法本身以超类为参数的方法?
我试图调用这个方法publicvoidsetnewproblem (problem problem )。 具体实现了抽象系统的问题.不幸的是,我得到了NoSuchMethodException异常.
我会呼叫这样的呼叫:
方法方法=model.getclass (.get method ) ' set'propertyname,newclass ) {newvalue.getclass;
method.invoke (模式,newValue );
如果我将newValue.getClass ()更改为Problem.class,一切正常。 什么想法将子类传递给公共void集新建problem )问题);
解决方法:
我必须问准确的类型。 这是因为有多种可能的重载方法,需要准确理解所需的内容。
所以可以在子类中调用,但不能不在那里请求子类
你能做的就是看所有的方法找到匹配。
如果只需要属性检索者或检索者,建议引用BeanIntrospector。 将找到此属性的所有属性和检索器/检索方法。
标签: superclass、invoke、java、reflection、subclass
资料来源: https://codeday.me/bug/2019 08 31/1774918.html