首页 文章

Groovy-Create实例声明对象类型

提问于
浏览
0

我有一个课程

Class LifeCycle{

  Activity  obtainPo
  Activity  bookTicket

}

Class Activity{
   String name
   string actor
}

Objective

Lifecycle 类中声明 Activity 类型的对象时,应该创建活动类的新实例,其中name = parameter name(即obtainPo和bookTocket等)就像

new Activity('obtainPo') actor可以为null

如何在groovy和grails中实现这一目标?

1 回答

  • 0

    当你实例化LifeCycle类对象并确保在构造函数中设置LifeCycle的其他属性(如果有的话)时,会发生这样的事情 .

    class LifeCycle{
    
      Activity obtainPo, bookTicket
      def anotherParameter
    
       LifeCycle(){
        //set another parameters first
          anotherParameter = "something"
    
        this.getProperties().each{
           if(!it.value){
             this.setProperty(it.key, new Activity(name:it.key))
           }
        }
       }
    }
    

相关问题