如果我的Java Method 是某个字段的获取者,我可以以某种方式覆盖它吗?
PropertyDescriptor[] propDescArr = Introspector
.getBeanInfo(myClass, Object.class)
.getPropertyDescriptors();
for (PropertyDescriptor pd : propDescArr) {
Method getMyField= pd.getReadMethod();
// ... override this method here somehow so it returns ""
// for example: getMyField.setDefaultReturnValue("");
}
....
public Class AnotherClass{
ClassThatHasChangedMethod example = new ClassThatHasChangedMethod();
String newBehavior = example.getMyField(); // newBehavior is now equals "" and before it was some other string.
}
所以我在其他正在起作用的类中使用了 method 方法。例如:方法 method 返回一些字符串。我要更改它,因此它返回“”。
稍后当我从另一个类调用此方法时,我希望它具有已更改的行为(返回“”)。