Java中没有withorand关键字。这些是方法调用。
在这里,该with()方法通过静态导入(请参阅@damo 的评论)导入并创建构建器类的实例;该and()方法是一个返回的方法this;然后.parameters()该类将构建该类,并返回调用方法实际需要的内容。这是一个经典的建造者模式。
静态导入可用于提高可读性效果。考虑模仿。要么你写:
Mockito.when(xxx).then(xxx);
或者:
import static org.mockito.Mockito.*;
// the compiler know where `when()` comes from
when(xxx).then(xxx);
这种构造的通用模式称为流式接口。为了完成答案,我只想指出我为我的一个项目所做的事情:
ProcessorSelector<IN, OUT> selector = new ProcessorSelector<IN, OUT>();
selector = selector.when(predicate1).then(p1)
.when(predicate2).then(p2)
.otherwise(defaultProcessor);
final Processor<IN, OUT> processor = selector.getProcessor();
该类ProcessorSelector有一个when()方法;此方法返回另一个有then()方法的类;并且该then()方法再次返回 a ProcessorSelector,它具有when()等。最后,该otherwise()方法返回 a ProcessorSelector,您在其上.getProcessor()返回 a Processor。