我最近在《行动》中读到Groovy。在第七章中,它引入了“*.aperor ” 。当我运行关于这个操作员的代码时,我犯了一些错误。
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,0,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)
])
]
//error
assert [5*1499, 499, 4*499] == invoices.items*.total()
The last line will throw a exception.
At first, i can explain why this error happend. The invocies is a List, and the element s type is Invoice. So directly using items will make a error. I attempt to fix it by using invoices.collect{it.items*.total()}
但还是有一个失败的主张。因此,我如何使主张成功以及为什么发票*.items*.tems*.tall ()会提出一个例外。