获取以下Groovy代码中的错误。。有什么问题吗?

获取以下Groovy代码中的错误。。有什么问题吗?,groovy,Groovy,大家好,下面的代码没有被执行,代码是 class Invoice{ List items Date date } class LineItem{ Product product int count int total(){ return product.dollar * count } } class Product{ String name

大家好,下面的代码没有被执行,代码是

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(name:'ULC', dollar:200)
def ve = new Product(name:'Visual Editor',dollar:500)
def invoices =[new Invoice(date:ulcDate, items: [new LineItem(count:5, product:ulc),new LineItem(count:2, product:ve)]), new Invoice(date:[107,1,1],items:[new LineItem(count:4,product:ve)])]
assert [200,500,400] == invoices.items*.total()
这个文件名是
Test.groovy
,当我像这样执行
groovy Test
时,我得到了如下错误:

Caught: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.total() is applicable for argument types: () values: []
Possible solutions: tail(), getAt(groovy.lang.Range), getAt(java.util.Collection), getAt(int), getAt(java.lang.String), getAt(java.lang.String)
    at In1.run(In1.groovy:20)

即使我已经定义了
total()
方法,它还是会抛出一个错误?原因是什么?

假设您从代码中键入了剪切粘贴,那么您就有一个输入错误

class LineItem{
    Product product
    int count
    int total**1**(){
            return product.dollar * count
    }
}

请从方法名称中删除“1”,然后重试。

如果您在代码中键入了剪切粘贴,则会出现键入错误

class LineItem{
    Product product
    int count
    int total**1**(){
            return product.dollar * count
    }
}
从方法名称中删除“1”,然后重试。

关于行:

assert [200,500,400] == invoices.items*.total()
invoices.items
返回一个
List
so
invoices.items*.total()
失败,因为外部
List
没有
total()
方法

如果要获取每个行项目的总数,只需调用
flant()

如果要获取每张发票的总价值,请尝试以下操作

assert [2000, 2000] == invoices.items*.sum { it.total() }
关于线路:

assert [200,500,400] == invoices.items*.total()
invoices.items
返回一个
List
so
invoices.items*.total()
失败,因为外部
List
没有
total()
方法

如果要获取每个行项目的总数,只需调用
flant()

如果要获取每张发票的总价值,请尝试以下操作

assert [2000, 2000] == invoices.items*.sum { it.total() }

或者
assert[20002000]==invoices.items*.sum{it.total1()}
@Don!你怎么说<代码>列表<代码>这是发票退回的东西。物品<代码>?@Ant你是问我怎么知道这是退回的东西,还是为什么要退回?@Don:不,我是问你怎么知道。。。。如果你能解释一下为什么这也会被返回,或者
assert[20002000]==invoices.items*.sum{it.total1()}
@Don!你怎么说<代码>列表<代码>这是发票退回的东西。物品<代码>?@Ant你是问我怎么知道这是退回的东西,还是为什么要退回?@Don:不,我是问你怎么知道。。。。如果你能解释一下为什么这也会被退回,那就好了……不过我的问题有错别字,对不起,这不是正确的答案,正确的答案是唐提到的+1票赞成恢复到0票。无论是谁投票否决了这一点都是不公平的-你必须按照问题提出的方式来回答。但是我的问题有拼写错误,很抱歉,这不是正确的答案,正确的答案是Don提到的+1票赞成恢复到0票。无论是谁投票否决了这一点都是不公平的——你必须对提出的问题做出回应。