Java 从基类继承Grails域类属性

Java 从基类继承Grails域类属性,java,grails,groovy,grails-domain-class,Java,Grails,Groovy,Grails Domain Class,我正在尝试创建一个动态继承另一个类属性的域类构造函数。但我不能让它正常工作 举个例子: class Example1 { String name; String location; } class Example2 extends Example1 { String status; public Example2 (Example1 orig){ // Code here to set this.name and this.location to name an

我正在尝试创建一个动态继承另一个类属性的域类构造函数。但我不能让它正常工作

举个例子:

class Example1 {

  String name;
  String location;
}

class Example2 extends Example1 {

  String status;

  public Example2 (Example1 orig){
    // Code here to set this.name and this.location  to name and location from orig
    // dynamically, so adding a field in Example1 does not require me to add that 
    // field here.
  }
}

我不完全清楚您想要完成什么,但有没有任何理由不能在“Example2”类中只包含一个“Example1”字段?

您工作太辛苦了,只需复制属性即可:

class Example2 extends Example1 {

   String status

   Example2() {}

   Example2(Example1 orig) {
      this.properties = orig.properties
   }
}

在进行了足够的故障排除和在线搜索后,我找到了一个解决方案,以防任何人都在寻找类似的解决方案:

public Example2(Example1 orig){
   def d = new DefaultGrailsDomainClass(Example1.class)
   d.persistentProperties.each { val ->
       this[val.name] = orig[val.name]         
   }       
}
包括:

import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass

我不想在多个类之间复制数据,这有点违背了扩展的目的。我提出了一个解决方案,它发布在上面。或者只是让Example1抽象(如果Example1的道具是您所需要的,请使用src/groovy);扩展示例2并继续。我试过了,它说我必须等8小时:(是的,有时间限制。也许明天再来做:)