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

Popular posts from this blog

தட்டு வண்டியில் காளிக்கோயில் - சிறுகதை

பிரேமாவின் பெண் குழந்தை

தீபம் பிளக்ஸ்