2011-04-02 28 views
2
/出不同水平的方法,通話的

比方說,我有:如何突破Java中

public void one() { 
    two(); 
    // continue here 
} 
public void two() { 
    three(); 
} 
public void three() { 
    // exits two() and three() and continues back in one() 
} 

是否有任何方法來這樣做呢?

回答

1

假設你可以更改two()方法,也許你想像這樣?

public void one() { 
    two(); 
    // continue here from condition 
} 

public void two() { 
    if (three()) { 
     // go back due to condition 
     return; 
    } 

    // condition wasn't met 
} 

public boolean three() { 
    // some condition is determined here 

    if (condition) { 
     // exits two() and three() and continues back in one() 
     return true; 
    } 

    // condition wasn't met, keep processing here 

    // finally return false so two() keeps going too 
    return false; 
} 
5

沒有改變方法two()的唯一方法是拋出一個異常。

如果你可以改變代碼,你可以返回一個布爾值,告訴調用者返回。

然而,最簡單的解決方案是將方法內聯到一個更大的方法。如果這太大,你應該重新構建它,而不是像這樣的方法之間放置複雜的控制。


說你有

public void one() { 
    System.out.println("Start of one."); 
    two(); 
// do something 
    System.out.println("End of one."); 
} 

public void two() { 
    System.out.println("Start of two."); 
    three(); 
// do something 
    System.out.println("End of two."); 
} 

public void three() { 
    System.out.println("Start of three."); 
// do something 
    System.out.println("End of three."); 
} 

您可以添加一個unchecked異常,如果你不能改變兩();

public void one() { 
    System.out.println("Start of one."); 
    try { 
     two(); 
    } catch (CancellationException expected) { 
    } 
// do something 
    System.out.println("End of one."); 
} 

public void two() { 
    System.out.println("Start of two."); 
    three(); 
// do something 
    System.out.println("End of two."); 
} 

public void three() { 
    System.out.println("Start of three."); 
// do something 
    System.out.println("End of three."); 
    throw new CancellationException(); // use your own exception if possible. 
} 

可以返回一個布爾說回報,如果可以改變兩()

public void one() { 
    System.out.println("Start of one."); 
    two(); 
// do something 
    System.out.println("End of one."); 
} 

public void two() { 
    System.out.println("Start of two."); 
    if (three()) return; 
// do something 
    System.out.println("End of two."); 
} 

public boolean three() { 
    System.out.println("Start of three."); 
// do something 
    System.out.println("End of three."); 
    return true; 
} 

或者你也可以內嵌結構

public void one() { 
    System.out.println("Start of one."); 
    two(); 
// do something 
    System.out.println("End of one."); 
} 

public void two() { 
    System.out.println("Start of two."); 
    System.out.println("Start of three."); 
// do something for three 
    System.out.println("End of three."); 
    boolean condition = true; 
    if (!condition) { 
// do something for two 
     System.out.println("End of two."); 
    } 
} 
+1

非常感謝。我想我會用返回的方法。代碼太大,難以縮小。 – Hamlyn 2011-04-02 08:35:03

+0

拋出異常正是我所需要的,但不知何故,起初我無法弄清楚,謝謝! – djule5 2011-10-19 04:08:28

1

展望在你的代碼中,如果你打電話給一個,那麼它會調用兩個,這叫三e .. 如果你保持原樣,那就是它會做的。兩行後(在你的一個)函數,將只會完成一次它從兩個回來,它不會這樣做,直到兩個已完成三個..

+1

+0:您必須假設這些方法的功能比代碼中建議的要多,否則最簡單的解決方案就是將它們刪除,因爲它們什麼都不做。 ;) – 2011-04-02 08:03:25

+0

的確,但可悲的是,我們只能使用我們擁有的代碼 – BugFinder 2011-04-02 08:04:49

+0

哈哈。我實際上已經簡化了這些方法。當然這些方法會更復雜,我希望能夠在條件下退出 – Hamlyn 2011-04-02 08:26:52