6

Groovy 允许在前端页面中使用字符串做一些好事,例如:

${"hello".capitalize()}

如何向String类添加新的自定义方法?像:

${"hello".custom()}

4

1 回答 1

7

使用元类

String.metaClass.custom = { //dosomething }

请参阅http://www.groovyexamples.org/2010/07/19/dynamically-add-properties-to-a-class/

于 2012-04-27T20:08:30.807 回答