
class Invoice { List items Date date } class lineItem { Product product int count int total() { return product.dollar * count } } class Product { String name def dollar } def ulcdate = new Date(107,1)def ulc = new Product(dollar:1499,name:'ulC') def ve = new Product(dollar:499,name:'Visual Editor') def invoices = [ new Invoice(date:ulcdate,items: [ new lineItem(count:5,product:ulc),new lineItem(count:1,product:ve) ]),new Invoice(date:[107,1,2],items: [ new lineItem(count:4,product:ve) ]) ] //errorassert [5*1499,499,4*499] == invoices.items*.total() 最后一行将抛出异常.
首先,我可以解释为什么会发生这种错误. invocIEs是List,元素的类型是Invoice.所以直接使用项目会出错.我尝试使用invoices.collect {it.items * .total()}来修复它
但仍然会失败断言.那么,我怎样才能使断言成功以及为什么发票* .items * .total()会抛出异常.
解决方法 发票*的结果. operator是一个列表,因此发票* .items的结果是一个列表列表. flatten()可以应用于列表并返回一个平面列表,因此您可以使用它从ListItem列表列表中创建lineItem列表.然后,您可以使用spread运算符将total()应用于其元素:assert [5*1499,4*499] == invoices*.items.flatten()*.total()总结
以上是内存溢出为你收集整理的Groovy *. *** 作符全部内容,希望文章能够帮你解决Groovy *. *** 作符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)