我有get and set class:
public static class Structure{
private String YOne = null;
private String YTwo = null;
public String getYOne() {
return YOne;
}
public void setYOne(String YOne) {
this.YOne = YOne;
}
public String getYTwo() {
return YTwo;
}
public void setYTwo(String YTwo) {
this.YTwo = YTwo;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我填写我的class:
Structure.setYOne("my value");
Structure.setYTwo("my value");
Run Code Online (Sandbox Code Playgroud)
我怎么能清空所有这些?
注意:我不喜欢一个一个地清空.
您可以在Structure类中实现一个方法,将所有字段设置为null使用Reflection:
public void clearFields() throws IllegalArgumentException, IllegalAccessException {
Field[] properties = this.getClass().getDeclaredFields();
for (Field f : properties) {
f.setAccessible(true);
f.set(this, null);
}
}
Run Code Online (Sandbox Code Playgroud)