从抽象类继承属性,并根据抽象属性对集合进行排序

发布于 2024-08-20 14:36:09 字数 1407 浏览 6 评论 0原文

我正在使用 Grails 创建发票管理应用程序,并且遇到继承问题。

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

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

我的域类的来源:

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
}

有问题的代码的来源:

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: No such property: Category for class: TimedItem

Stacktrace 表示上面示例的第 6 行。

I am creating an invoice management application using Grails, and am experiencing problems with inheritance.

If is my intention that each invoice should contain a collection of lines/items, and that when the invoice is formatted for printing, the items are sorted by date, separated into lists by category, then have the price of each line calculated in a different way for each concrete type (Timed items will lookup the hourly in the rates property, Priced items are assigned a price on creation).

Node Invoice has a property "items", which is a collection of Item objects.

Source of my domain classes:

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
}

Source of the problematic code:

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)
    }
}

Error message:
groovy.lang.MissingPropertyException: No such property: category for class: TimedItem

Stacktrace indicates the 6th line of the above example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-08-27 14:36:09

您使用枚举错误。 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")
    }
}

You are using enum wrong. The enum keyword is similar to the class keyword. So while you defined your enum type, you never gave your class an instance of it. While you could leave the definition of the enum inside the abstract Item class, I moved it outside for clarity.

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")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文