特质
生成者模型,无输入参数,返回指定类型
测试
测试参照
@Data
public static class Foo {
String strVal = "Hello";
int intVal = 1;
Double doubleVal = 2.0;
Long longVal = 3L;
Boolean trueVal = true;
String strTmp;
int intTmp = 1;
Double doubleTmp;
Long longTmp;
}
万能型Supplier
可以指定返回类型。
Supplier<Foo> supplier = Foo::new;
Assert.assertTrue(supplier.get().trueVal);
固定类型Supplier
无需指定输出类型。
BooleanSupplier booleanSupplier = () -> Boolean.TRUE;
Assert.assertTrue(booleanSupplier.getAsBoolean());
DoubleSupplier doubleSupplier = () -> foo.doubleVal;
assertEquals(foo.doubleVal, foo.doubleVal, doubleSupplier.getAsDouble());
IntSupplier intSupplier = () -> foo.intVal;
assertEquals(foo.intVal, intSupplier.getAsInt());
LongSupplier longSupplier = () -> foo.longVal;
assertEquals(foo.longVal.longValue(), longSupplier.getAsLong());
版权声明:本文为cx105200原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。