访问setName方法报错,怎么改正 Constructor constructor = clazz.getConstructor(); Object o = constructor.newInstance(); Method setName = clazz.getMethod("setName",clazz); setName.invoke(o,"小猫"); System.out.println(o);
时间: 2024-02-25 14:53:08 浏览: 23
这段代码中,第三行的 `clazz.getMethod("setName",clazz)` 中,第二个参数应该是 `String.class`,而不是 `clazz`,因为 `setName` 方法的参数类型是 `String` 而不是 `Class`。
修改后的代码如下所示:
```
Constructor constructor = clazz.getConstructor();
Object o = constructor.newInstance();
Method setName = clazz.getMethod("setName", String.class);
setName.invoke(o, "小猫");
System.out.println(o);
```
这样就可以正确地调用 `setName` 方法并为对象设置名字了。
相关问题
package step4; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Reflect_stu { public static void main(String[] args) throws InvocationTargetException { //使用反射调用 Class clazz = null; try { clazz = Class.forName("step4.Apple"); /********** Begin *********/ Method setPriceMethod = clazz.getMethod("setPrice", double.class); Constructor appleConstructor = clazz.getConstructor(); Object apple = appleConstructor.newInstance(); setPriceMethod.invoke(apple, 14); Method getPriceMethod = clazz.getMethod("getPrice"); System.out.println(getPriceMethod.invoke(apple)); Method getTotal = clazz.getMethod("getTotal", double.class, int.class); System.out.println(getTotal.invoke(apple, 20, 24)); /********** End *********/ } catch (Exception e) { e.printStackTrace(); } } } class Apple { private double price; private int count; public Apple() { } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public double getTotal(double price, int count) { return price * count; } }
这段代码是一个使用反射调用的示例,它包括一个主类 Reflect_stu 和一个 Apple 类。在主类中,首先使用 Class.forName 方法获取 Apple 类的 Class 对象,然后使用 getMethod 方法获取 Apple 类中的 setPrice、getPrice 和 getTotal 方法,并使用 getConstructor 方法获取 Apple 类的默认构造方法的 Constructor 对象。接着使用 newInstance 方法创建一个 Apple 对象,并使用 invoke 方法调用 setPrice 方法设置 Apple 对象的价格为 14,使用 invoke 方法调用 getPrice 方法获取 Apple 对象的价格并输出,最后使用 invoke 方法调用 getTotal 方法计算 Apple 对象的总价并输出。
(1)定义一个标准的Person类,包含属性变量name,并包含属性对应的setXX/getXX方法(如setName方法和getName方法)、空参构造。 (2)使用反射的方式创建一个Person实例对象。 (3)使用反射方式调用setName()方法对name的值进行设置。 (4)设置之后,在通过反射方式执行getName()方法。
下面是Java代码示例:
```java
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
public class Person {
private String name;
public Person() {}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) throws Exception {
// 使用反射创建Person对象
Class<?> clazz = Class.forName("Person");
Constructor<?> constructor = clazz.getConstructor();
Object person = constructor.newInstance();
// 使用反射调用setName方法设置name属性的值
Method setNameMethod = clazz.getMethod("setName", String.class);
setNameMethod.invoke(person, "Tom");
// 使用反射调用getName方法获取name属性的值
Method getNameMethod = clazz.getMethod("getName");
String name = (String) getNameMethod.invoke(person);
System.out.println("Name: " + name); // 输出结果:Name: Tom
}
}
```