2019年2月17日 在一本书上看到这段代码,很好奇是什么意思?Assert:断言机制:测试代码或者调试程序时,总会做出一些假设,断言就是用于在代码中捕捉这些假设。当要判断一个方法传入的参数时,我们就可以使用断言。例如:public Order create(Cart cart, Receiver receiver, PaymentMethod paymentMethod, ShippingMethod shippingMethod, BoxMethod boxMethod, CouponCode couponCode, boolean isInvoice) { Assert.notNull(cart); Assert.notEmpty(cart.getCartItems()); Assert.isTrue(cart.checkedSize()>0, “购物项选择必须大于0”); Assert.notNull(receiver); Assert.notNull(paymentMethod); Assert.notNull(shippingMethod);}这样可以检测传入的参数是否符合要求,当这些断言方法在入参不满足要求时就会抛出 IllegalArgumentException。Assert.notNULL()断言常用的方法notNull(Object object) notNull(Object object, String message) 该函数的意思是传入的object必须不能为空。如果为空就抛出异常。与 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null。如果不是,则会报错。