Java 中的关键字 break
可用于中断循环或 switch 语句。有什么东西可以用来打破方法吗?
最佳答案
使用 return
退出方法的关键字。
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return;
}
//... otherwise do the following...
}
来 self 上面链接的 Java 教程:
Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:
return;
关于java - 如何打破或退出Java中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937029/