Groovy - Expando Dynamic bean Class like Object in JS
/*
Expando
The Expando class is a dynamic bean so we can add properties and we can add
closures as methods to an instance of this class
http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
def user = new Expando(name:"Roberto")
assert 'Roberto' == user.name
user.lastName = 'Pérez'
assert 'Pérez' == user.lastName
user.showInfo = { out ->
out << "Name: $name"
out << ", Last name: $lastName"
}
def sw = new StringWriter()
println user.showInfo(sw)
Comments
Post a Comment