編輯:如果if
或else if
之一是真的,我只想要執行finally
代碼。Java - '最後'相當於if語句
很多很多次,我遇到以下情況:
if (condition1)
do stuff
do something
else if (conditon2)
do other stuff
do something
else if (condition3)
algorithm here
do something
如果有一個finally
子句的if語句,我可以減少到:
if (condition1)
do stuff
else if (conditon2)
do other stuff
else if (condition3)
algorithm here
finally
do something
我想找到一些解決方案,其中只需要調用一次do something
,而不需要額外的方法,如果聲明或做出如下標誌,會發生一些不合適的地方:
boolean special = false;
if (condition1)
do stuff
special = true;
else if (conditon2)
do other stuff
special = true;
else if (condition3)
algorithm here
special = true;
if (special) // <--- bad solution, doesn't simplify anything
do something
難道你不能只在'if'和'else'後面加上'doSomething'嗎? –
爲什麼不放''做點什麼'畢竟如果,它只會執行一次,最後。 – Mritunjay
@Mritunjay,因爲我認爲他不想執行它,如果條件1,條件2和條件3都是假的 – ajb