我想不出正确的方法来拥有一个 Guava Optional 的默认构造函数参数。以下代码无法使用 Java 1.6 编译
public class ShoppingCart implements Serializable {
private final Optional<DiscountCoupon> discountCoupon;
public ShoppingCart() {
this(Optional.absent());
}
public ShoppingCart(Optional<DiscountCoupon> dc) {
Preconditions.checkNotNull(dc);
if (dc.isPresent()) {
Preconditions.checkArgument(dc.get().getPennyDiscount() != 0 ||
dc.get().getPercentDiscount() != 0);
}
discountCoupon = dc;
}
报告的错误是:
/Users/pfarrell/sandbox//com/wayfinderdigital/struct/ShoppingCart.java:29: cannot find symbol
symbol : constructor ShoppingCart(com.google.common.base.Optional<java.lang.Object>)
location: class com.wayfinderdigital.struct.ShoppingCart
this(Optional.absent());