- Extension Method 用于对已存在的类在其外部添加扩展方法,该功能类似于ruby语言中的mixin特性。
- JFinal Template Engine 默认已经为String、Integer、Long、Float、Double、Short、Byte 这七个基本的 java 类型,添加了toInt()、toLong()、toFloat()、toDouble()、toBoolean()、toShort()、toByte() 七个extension method。以下是使用示例:
#set(age = "18")
#if(age.toInt() >= 18)
join to the party
#end
- 上例第一行代码中的age为String类型,由于String被添加了toInt()扩展方法,所以调用其toInt()方法后便可与后面的Integer型的值18进行比较运算。
- 下方代码为项目种实际应用,json.charge得到的值是String格式的 然后转成Double的格式
#number(json.charge.toDouble()/100000,'#.##')