【问题标题】:creating groovy classes创建 groovy 类
【发布时间】:2012-10-01 22:01:04
【问题描述】:

我正在尝试学习如何创建简单的 groovy 类,它们不是域类。我想在我的软件中创建一组类(及其对象),但无意保存到数据库中。具体来说,我有一个关于如何创建一个具有第二类列表属性的类的问题。像这样:

class oParent{
    def fName
    def lName
    def listOfChildren
}
class oChild{
    def propertyOne
    def propertyTwo
}

所以,通过这个例子,我可以像这样创建每个对象:

def p = new oParent();
def cOne = new oChild();
def cTwo = new oChild();

p.fName ="SomeName"
p.lName ="some Last Name"

cOne.propertyOne = "a"
cOne.propertyTwo = "b"

cTwo.propertyOne = "c"
cTwo.propertyTwo = "d"

那么,我如何将每个子对象(cOne 和 cTwo)添加到父对象 p)。添加后,我将如何遍历父类的子类属性,例如打印所有子类的所有 propertyTwo 属性?

【问题讨论】:

  • 投反对票有什么特别的原因吗?
  • 可能是因为奇怪的类和实例名称。投反对票的小理由,但你永远不知道。

标签: grails groovy


【解决方案1】:

这是您的代码的注释版本,其中包含一些更改建议:

// 1) Class names start with a Capital Letter
class Parent {

  // 2) If you know the type of something, use it rather than def
  String fName
  String lName

  // 3) better name and type
  List<Child> children = []

  // 4) A utility method for adding a child (as it is a standard operation)
  void addChild( Child c ) {
    children << c
  }

  // 5) A helper method so we can do `parent << child` as a shortcut
  Parent leftShift( Child c ) {
    addChild( c )
    this
  }

  // 6) Nice String representation of the object
  String toString() {
    "$fName, $lName $children"
  }
}

// See 1)
class Child{

  // See 2)
  String propertyOne
  String propertyTwo

  // See 6)
  String toString() {
    "($propertyOne $propertyTwo)"
  }
}

// Create the object and set its props in a single statement
Parent p = new Parent( fName: 'SomeName', lName:'some Last Name' )

// Same for the child objects
Child cOne = new Child( propertyOne: 'a', propertyTwo: 'b' )
Child cTwo = new Child( propertyOne: 'c', propertyTwo: 'd' )

// Add cOne and cTwo to p using the leftShift helper in (5) above
p << cOne << cTwo

// prints "SomeName, some Last Name [(a b), (c d)]"
println p

然后,您可以这样做:

println p.children.propertyTwo // prints "[b, d]"

或者这个:

p.children.propertyTwo.each { println it } // prints b and d on separate lines

或者,确实是这样:

p.children.each { println it.propertyTwo } // As above

【讨论】:

    【解决方案2】:

    好吧,事实证明这很简单,除非我做错了。我是这样做的:

    class oParent{
       def fName
       def lName
       def listOfChildren = []
    }
    
    class oChild{
       def propertyOne
       def propertyTwo
    }
    
    def p = new oParent();
    def cOne = new oChild();
    def cTwo = new oChild();
    
    p.fName ="SomeName"
    p.lName ="some Last Name"
    
    cOne.propertyOne = "a"
    cOne.propertyTwo = "b"
    
    cTwo.propertyOne = "c"
    cTwo.propertyTwo = "d"
    
    p.listOfChildren.add(cOne)
    p.listOfChildren.add(cTwo)
    

    我可以这样迭代:

    p.listOfChildren.each{ foo->
        log.debug(foo.propertyOne)
    }
    

    【讨论】:

    • 你是对的。将其命名为 children 而不是 listOfChildren 更为典型,但想法是一样的。
    • 更典型的做法是给 oParent(通常命名为“Parent”)一个 addChild 方法,而不是直接访问父列表。
    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多