Groovy allows to do some nice things with strings in frontend pages, like:
${"hello".capitalize()}
How can I add a new custom method to the String
class? Like:
${"hello".custom()}
You cannot extend a class that is marked as final. You can use composition to either put a String object inside or you can hand roll your own version. This can be accomplished via character arrays and the other magic that goes into creating String classes.
Yes, you may intermix Java and Groovy sources in a project and have source dependencies in either direction, including "extends" and "implements".
In Groovy, we can add a method named call to a class and then invoke the method without using the name call . We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: () . This can be especially useful in for example a DSL written with Groovy.
Use the metaClass
String.metaClass.custom = { //dosomething }
See http://www.groovyexamples.org/2010/07/19/dynamically-add-properties-to-a-class/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With