如何在 Java 中中断/退出不同级别的方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5521967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to break/exit different levels of method-calling in Java
提问by Hamlyn
Let's say I have:
假设我有:
public void one() {
two();
// continue here
}
public void two() {
three();
}
public void three() {
// exits two() and three() and continues back in one()
}
Are there any methods to doing this?
有没有办法做到这一点?
采纳答案by WhiteFang34
Assuming you can change the two()
method, perhaps you want something like this?
假设您可以更改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;
}
回答by Peter Lawrey
The only way to do this without change method two() is to throw an Exception.
在不更改方法 two() 的情况下执行此操作的唯一方法是抛出异常。
If you can change the code you can return a boolean which tells the caller to return.
如果您可以更改代码,您可以返回一个布尔值,告诉调用者返回。
However the simplest solution is to inline the methods into one larger method. If this is too large you should retsructure it another way and not place complex controls between methods like this.
然而,最简单的解决方案是将方法内联到一个更大的方法中。如果它太大,您应该以另一种方式重新构建它,而不是在这样的方法之间放置复杂的控件。
Say you have
说你有
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.");
}
You can add an unchecked exception if you cannot change two();
如果你不能改变 two() ,你可以添加一个未经检查的异常;
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.
}
You can return a boolean to say return, if you can change two()
如果您可以更改 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.");
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;
}
Or you can inline the structures
或者你可以内联结构
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.");
}
}
回答by BugFinder
Looking at your code, if you call one, it then calls two, which calls three.. If you leave it as it is, thats exactly what it WILL do. The line after two (in your one) function, will be only done once its come back from two, and it wont do that until two has finished with three..
看看你的代码,如果你调用一个,它然后调用两个,调用三个..如果你保持原样,这正是它会做的。两个(在你的一个)函数之后的那一行,只有在它从两个返回后才会完成,并且它不会这样做,直到两个完成三个......