class Game{
String name;
String price;
String type;
public Game(String name, String price, String type) {
this.name = name;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Game game = new Game("最终幻想", "88", "月卡");
Class<? extends Game> cookClass = game.getClass();
Field[] fields = cookClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cookClass);
Method methodGet = pd.getReadMethod();
Method methodSet = pd.getWriteMethod();
System.out.println(field.getName()+":" + methodGet.invoke(game));
methodSet.invoke(game,"128");
System.out.println(field.getName()+":" + methodGet.invoke(game));
}