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?
well, it turned out to be pretty simple, unless i’m doing it wrong. here is how i did it:
I can the iterate like this: