为什么不能在参数中使用Java 8的可选性?
public int calculateSomething(Optional<String> p1, Optional<BigDecimal> p2 { // my logic}
public int calculateSomething(String p1, BigDecimal p2) { // my logic}
public int calculateSomething() { calculateSomething(null, null);}public int calculateSomething(String p1) { calculateSomething(p1, null);}public int calculateSomething(BigDecimal p2) { calculateSomething(null, p2);}public int calculateSomething(String p1, BigDecimal p2) { // my logic}
Optional
calculateSomething
Optional<String> p1 = otherObject.getP1();Optional<BigInteger> p2 = otherObject.getP2();int result = myObject.calculateSomething(p1, p2);
Optional<String> p1 = otherObject.getP1();Optional<BigInteger> p2 = otherObject.getP2(); int result = myObject.calculateSomething(p1.orElse(null), p2.orElse(null));
Optional<String> p1 = otherObject.getP1();Optional<BigInteger> p2 = otherObject.getP2();int result;if (p1.isPresent()) { if (p2.isPresent()) { result = myObject.calculateSomething(p1, p2); } else { result = myObject.calculateSomething(p1); }} else { if (p2.isPresent()) { result = myObject.calculateSomething(p2); } else { result = myObject.calculateSomething(); }}
Optional
Optional
呼啦一阵风
米琪卡哇伊
相关分类