以不同的行为解决价值,最好没有其他行为
这是我想做的
首先启动可选变量,然后执行过程。 如果程序a收益率为null,请尝试过程B,依此类推。 如果所有过程仍然产生无效,请抛出例外情况,
#pseudocode
Optional<> someVariable;
if (someVariable is empty):
fetch value through procedure A
assign to someVariable
if (someVariable is empty):
fetch value through procedure B
assign to someVariable
.
.
.
if still empty:
throw exception
问题是我真的不想通过已解决该值的过程A,并且如果添加更多过程,我认为它是可扩展的 有任何输入吗?
Here is what I'm trying to do
First initiate an optional variable, then do procedure A.
if procedure A yield null then try procedure B and so on.
if all the procedure still yield null, throw exception
#pseudocode
Optional<> someVariable;
if (someVariable is empty):
fetch value through procedure A
assign to someVariable
if (someVariable is empty):
fetch value through procedure B
assign to someVariable
.
.
.
if still empty:
throw exception
The problem is I don't really want to go through procedure B if procedure A already resolve the value and I don't think it is scalable if more procedure is added
any input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这没有其他
尝试更改方法返回的值的值。
This has no else
Try changing the values of what the methods return.
使用JDK 11(可能是JDK 8+):
这几乎与其他人建议的方法相同,但是使用最新的Java,您可以编写比我在其他答案中看到的代码更紧凑和可以理解的。
With JDK 11 (probably JDK 8+):
This is pretty much the same approach everybody else has suggested, but with the latest Java you can write code that's much more compact and understandable than the one I've seen in the other answers.
一个详细的,但希望可以阅读的方法:
A slightly verbose but hopefully readable approach:
如果您的过程相对简单,并且可以称为
运行
s。然后,我们可以创建一个可运行的列表,并使用for loop执行运行
方法:如果过程有些复杂,并且会返回值或进行参数,那么我们可以使用
callable 或使用运行方法定义自定义
过程
接口。If your procedures are relatively simple and can be declared as
Runnable
s. Then we can create a list of Runnables and execute therun
method with a for loop:If the procedures are somewhat complicated, and will return values or take arguments, then we can use
Callable
or define a customProcedure
interface with a running method.