2010-02-09 65 views
0

我正在使用Grails创建发票管理应用程序,并且遇到继承问题。从抽象类继承属性,并在抽象属性上对集合进行排序

如果我的意图是,每张发票都应该包含一行/项目集合,并且发票格式化为打印时,项目按日期排序,按类别分成列表,然后确定每行的价格以不同的方式为每个具体类型计算(定时项目将在费率属性中查找每小时,在创建时,定价项目会被分配一个价格)。

节点发票有一个属性“items”,它是Item对象的集合。

来源我的领域类:

invoiceInstance.items.add(new TimedItem(description:"waffle", minutes:60, date:new Date(),category:"OTHER")) 
def firstList = [] 
def lastList = [] 
invoiceInstance.items.sort{it.date} 
invoiceInstance.items.each(){ 
    switch(((Item)it).category){ 
     case "LETTER": 
      firstList.add(it) 
     break; 
     default: 
      lastList.add(it) 
    } 
} 

错误消息:
groovy.lang.MissingPropertyException:

class Invoice { 
    static constraints = { 
    }   
    String client 

    Date dateCreated 
    Date lastUpdated 
    CostProfile rates 

    def relatesToMany = [items : Item] 
    Set items = new HashSet() 
} 

abstract class Item{ 
    static constraints = { 
    } 
    String description 
    Date date 
    enum category {SERVICE,GOODS,OTHER} 
    def belongsTo = Invoice 
    Invoice invoice 
} 

class TimedItem extends Item{ 

    static constraints = { 
    } 

    int minutes 
} 

class PricedItem extends Item{ 

    static constraints = { 
    } 

    BigDecimal cost 
    BigDecimal taxrate 
} 

问题代码的来源没有这样的属性:类别类: TimedItem

Stacktrace指示第6行上面的例子。

+0

是否有原因转换为物品?这对我来说似乎没有必要。 – Snake 2010-02-09 14:03:46

回答

1

您使用的是枚举错误。 enum关键字与class关键字类似。所以当你定义你的枚举类型时,你从来没有给过类的实例。虽然您可以将枚举的定义保留在抽象Item类中,但为了清晰起见,我将它移出了外部。

class Invoice { 
    Set items = new HashSet() 
} 

enum ItemCategory {SERVICE,GOODS,OTHER} 

abstract class Item{ 
    String description 
    ItemCategory category 
} 

class TimedItem extends Item{ 
    int minutes 
} 


def invoice = new Invoice() 
invoice.items.add(new TimedItem(description:"waffle", minutes:60, category: ItemCategory.OTHER)) 

invoice.items.each(){ 
    switch(it.category){ 
     case ItemCategory.OTHER: 
      println("Other found") 
     break 
     default: 
      println("Default") 
    } 
} 
+0

我把枚举定义在grails-app/domain/ItemCategory.groovy中 在线: invoice.items.add(new TimedItem(description:“waffle”,minutes:60,category:ItemCategory.OTHER)) 我得到这个: groovy.lang.MissingPropertyException:没有这样的属性:类的ItemCategory:InvoiceController 我需要某种导入语句来访问ItemCategory的魔术值吗? – Emyr 2010-02-10 10:30:32

+0

我相信这是正确的。 import a.package.ItemCategory – Blacktiger 2010-02-10 16:29:10

+0

好的,我还没有在这个应用程序中声明任何包,我需要添加一个只是为了这个枚举? – Emyr 2010-02-11 09:58:20