creating groovy classes
Solution 1
well, it turned out to be pretty simple, unless i'm doing it wrong. here is how i did it:
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)
I can the iterate like this:
p.listOfChildren.each{ foo->
log.debug(foo.propertyOne)
}
Solution 2
Here's a commented version of your code with some suggestions for changes:
// 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
Then, you can do this:
println p.children.propertyTwo // prints "[b, d]"
Or this:
p.children.propertyTwo.each { println it } // prints b and d on separate lines
Or, indeed this:
p.children.each { println it.propertyTwo } // As above
jason
Results-oriented project manager and innovative software developer experienced with multi-cultural international aid organizations, government offices, and NGO’s. Innovative data management specialist able to see the business and technical sides of a problem with an eye for streamlining work, reducing cost, and maintaining data integrity. Proven leadership, with a strong emphasis on negotiation and problem resolution abilities. Demonstrated ability to acquire technical knowledge and skills rapidly.
Updated on June 14, 2022Comments
-
jason almost 2 years
I'm trying to learn how to create simple groovy classes, which are not domain classes. I want to create a set of classes (and their objects) in my software but have no intention of saving to a database. Specifically, I have a question about how to create a class which has a property that is a list of a second class. like this:
class oParent{ def fName def lName def listOfChildren } class oChild{ def propertyOne def propertyTwo }
So, with this example, I can create an object of each like this:
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"
So, how do i add each of the children objects (cOne and cTwo) to the parent object p). Once added, how would i then traverse the parent class's children's property and, for example, print all the propertyTwo properties for all children classes?
-
erturne over 11 yearsYou are correct. It's more typical to name it children rather than listOfChildren, but the idea is the same.
-
GreyBeardedGeek over 11 yearsIt would also be more typical to give oParent (which would typically be named 'Parent'), an addChild method instead of accessing the parent's list directly.