我按名称获取类,我需要用各自的数据更新它们,我的问题是如何用 java 来做我想添加一些虚拟数据的方法。我不知道类类型我只是获取类名并使用反射来获取他的数据
我使用此代码来获取类实例和
Class<?> classHandle = Class.forName(className);
Object myObject = classHandle.newInstance();
// iterate through all the methods declared by the class
for (Method method : classHandle.getMethods()) {
// find all the set methods
if (method.getName().matches("set[A-Z].*")
并且知道我找到了我想用数据更新它的 set 方法的列表,我该怎么做。
假设在类名中我得到了人,并且该类有 setSalary 和 setFirstName 等,我该如何通过反射来设置它们?
public class Person {
public void setSalery(double salery) {
this.salery = salery;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
}