[英]Java - java.lang.NoSuchMethodException
我尝试使用这个代码( 更新 m_set在for循环中使用,它通过几个使用不同类型参数的方法。如果我在getMethod
添加例如int.class,我会在一次迭代后得到错误,因为下一个方法需要String.class。是否可以使用反射来解决这个问题?):
Method m_set = product.getClass().getMethod(method_name);
m_set.invoke(product, method_value);
我收到此错误:
Exception in thread "main" java.lang.NoSuchMethodException: test.NormalChair.setHeight()
at java.lang.Class.getMethod(Class.java:1655)
at test.ProductTrader.create(ProductTrader.java:68)
at test.Test.main(Test.java:32)
错误地表明它试图在类中找到我使用此方法的方法。 但是该方法在父类中,它是公共方法。 我知道如果我使用getDeclaredMethod
,它会给出类似的错误,但是为什么它使用getMethod
给出了这个错误?
我的班级有这种方法:
public abstract class AbstractChair {
public String name;
public int height;
public AbstractChair() {
}
public AbstractChair(String name, int height){
this.name = name;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
我尝试使用此方法的类:
public class NormalChair extends AbstractChair {
public NormalChair() {
super();
}
public NormalChair(String name, int height) {
super(name, height);
}
// Copy constructor
public NormalChair(NormalChair chair) {
this(chair.getName(), chair.getHeight());
}
}
UPDATE2
如果我做这样的事情:
if(method_name == "setHeight"){
Method m_set = product.getClass().getMethod(method_name, int.class);
m_set.invoke(product, method_value);
}
else if (method_name == "setName")
{
Method m_set = product.getClass().getMethod(method_name, String.class);
m_set.invoke(product, method_value);
}
然后错误消失。 有人可以提出更普遍的方法吗?
您似乎忘记了传递您的方法所需的参数类型(请记住,方法可以使用不同的参数类型重载)。 看看你的代码,那里没有setHeight()
方法,但setHeight(int)
。 你应该尝试类似的东西
Method m_set = product.getClass().getMethod(method_name,method_value.getClass());
m_set.invoke(product, method_value);
由于你可能遇到基本类型的问题,你可以使用方法。 假设您想要在类中只有一个具有相同名称的方法,您可以迭代所有公共方法,将其名称与您要查找的方法进行比较,然后使用您想要的参数调用它。 就像是
Method[] methods = product.getClass().getMethods();
for (Method m : methods){
System.out.println(m);
if (m.getName().equals("setHeight")){
m.invoke(product, method_value);
break;
}
}
另一种可能更好的方法是使用类似于PropertyDescriptor
java.bean
包中的类。 感谢这个课程,你可以找到特定属性的getter和setter。 请注意, setHeight
属性是height
因此您需要使用它
Method setter = new PropertyDescriptor("height", product.getClass()).getWriteMethod();
setter.invoke(product, method_value);
使用product.getClass().getMethod("setHeight", int.class);
。 您必须传递方法参数类型以定位方法签名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.