舉止異常我有一個Java函數的以下結構:遞歸具有反射
public void recursiveFun(Object currentReturnValue, int numRecursiveCalls) {
for(Method currentMethod: currentReturnValue.getClass().getMethods()) {
String methodName = currentMethod.getName();
// base case
if(methodName.equals("getObject")) {
Object retVal = currentMethod.invoke(currentReturnValue, null);
System.out.println(retVal);
return;
}
else {
numRecursiveCalls++;
currentReturnValue = currentMethod.invoke(currentReturnValue, null);
recursiveFun(currentReturnValue, numRecursiveCalls);
boolean previousFrame = true;
}
}
我設置2個斷點,一個在基體殼體和第二在previousFrame =真。它首先停在我的基本案例中,然後我繼續前進。我發現它會返回到前一幀,因爲它將previousFrame設置爲true,但currentReturnValue的類型保持不變!它應該是一個不同的類型。
例如,Location類有一個getIdNum(),它返回一個MyInteger類型的對象。 MyInteger有一個返回對象的getObject()方法。在我的情況下,return語句應該彈出框架,currentReturnValue是一個MyInteger,並返回到currentReturnValue是Location的框架。
建議:使用日誌記錄機制或至少使用System.out.println()來轉儲依次發生的事情。部分問題可能只是您的調試器令您困惑。 –