0

Grailsを使用して請求書管理アプリケーションを作成していますが、継承で問題が発生しています。

各請求書に行/アイテムのコレクションを含める必要があり、請求書が印刷用にフォーマットされている場合、アイテムは日付で並べ替えられ、カテゴリごとにリストに分けられ、各行の価格が異なる方法で計算されるようにすることが私の意図である場合具体的なタイプごとの方法(時間指定されたアイテムはratesプロパティで1時間ごとに検索され、価格設定されたアイテムには作成時に価格が割り当てられます)。

Node Invoiceには、Itemオブジェクトのコレクションであるプロパティ「items」があります。

私のドメインクラスのソース:

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行目を示しています。

4

1 に答える 1

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")
    }
}
于 2010-02-09T19:34:57.870 に答える