从抽象类继承属性,并根据抽象属性对集合进行排序
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用枚举错误。 enum 关键字与 class 关键字类似。因此,当您定义枚举类型时,您从未给您的类提供它的实例。虽然您可以将枚举的定义保留在抽象 Item 类中,但为了清楚起见,我将其移到了外部。
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.