示例1:
UserInfo 类
package org.example;
import java.io.Serializable;
public class UserInfo implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserInfo{" +
"name='" + name + '\'' +
'}';
}
public static UserInfo of(String name) {
UserInfo userInfo = new UserInfo();
userInfo.setName(name);
return userInfo;
}
}
生成对象
方式1:
@Test
public void test_reflect() throws IllegalAccessException, InstantiationException {
UserInfo userInfo = UserInfo.class.newInstance();
userInfo.setName("x");
System.out.println(userInfo);
}
执行结果:
UserInfo{name='x'}