我不太确定你的意思,但我猜这someMethod
会调用toString
它给定的对象数组的元素。
如果是这种情况,那么您可以使用包含预期方法fobjects
的包装子类来包装每个元素。ForeighObject
toString
例如
//Subclass which wraps a ForeighObject instance..
class ForeighObjectWrapper extends ForeighObject {
ForeighObject wrapped;
ForeighObjectWrapper(ForeighObject toWrap){
super(/* get values from toWrap */);
this.wrapped = toWrap;
}
//Add the custom behaviour..
public String toString(){
return "Some custom behaviour: " + wrapped.toString();
}
}
ForeighObject[] fobjects = SomeStorage.getObjects(); //as before..
ForeighObject[] fobjectsWrapped = new ForeighObject[fobjects.length];
for(ForeighObject ob : fObjects){
fobjectsWrapped.add(new ForeighObjectWrapper(ob));
}
//then call someMethod on fobjectsWrapped instead of fobjects..
someMethod(fobjectsWrapped); //will invoke custom behaviour..
请注意,如果 someMethod 还依赖于包装实例的其他方法/属性的行为,那么您需要覆盖这些方法,以便它们按预期工作。