13.2Groovy使用ExpandoMetaClass注入方法
来源:互联网 发布:修改网卡mac的软件 编辑:程序博客网 时间:2024/05/21 11:35
如果想为类添加多个方法,可以使用ExpandoMetaClass(EMC)DSL语法。
Integer.metaClass { // 方法1 daysFormNow = { -> Calendar today = Calendar.instance today.add(Calendar.DAY_OF_MONTH, delegate) today.time } // 方法2 getDaysFromNow = { -> Calendar today = Calendar.instance today.add(Calendar.DAY_OF_MONTH, delegate) today.time } // 方法3 'static' { isEven = { val -> val % 2 == 0} } // 方法4 constructor = { Calendar calendar -> new Integer(calendar.get(Calendar.DAY_OF_YEAR)) } // 方法5 constructor = { int val -> println "Intercepting constructor call" constructor = Integer.class.getConstructor(Integer.TYPE) constructor.newInstance(val) }}println "方法1:" + 1.daysFormNow()println "方法2:" + 2.getDaysFromNow()println "方法3:" + Integer.isEven(3)println "方法4:" + new Integer(Calendar.instance)println "方法5:" + new Integer(5)
运行结果:方法1:Thu Nov 23 10:58:00 CST 2017
方法2:Fri Nov 24 10:58:00 CST 2017
方法3:false
Intercepting constructor call
方法4:326
Intercepting constructor call
方法5:5
阅读全文
0 0
- 13.2Groovy使用ExpandoMetaClass注入方法
- Groovy探索之MOP 四 使用ExpandoMetaClass来实现Mixin
- Groovy方法注入
- 实战 Groovy: 使用闭包、ExpandoMetaClass 和类别进行元编程
- Groovy探索之MOP 三 Class、MetaClass和ExpandoMetaClass
- Groovy元编程MOP(ExpandoMetaClass:EMC)的应用场景
- Groovy方法拦截,注入,合成,委托和动态类
- 使用闭包、ExpandoMetaClass 和类别进行元编程
- Groovy 分类注入
- Groovy EMC注入
- Groovy 实例注入
- Groovy Mixin注入
- 12.1Groovy使用GroovyInterceptable拦截方法
- 12.2Groovy使用MetaClass拦截方法
- Groovy方法
- Groovy探索 使用集合方法,写出更加Groovy风格的代码
- Groovy探索 使用集合方法,写出更加Groovy风格的代码
- Groovy高效编程——‘匕首方法’的使用
- docker 安装confluence 6.3.4 破解
- 懒加载和预加载
- 计算机图形学 变换矩阵
- php读取整个文件各函数比较
- 小程序获取用户openid,php获取微信小程序openid的方法
- 13.2Groovy使用ExpandoMetaClass注入方法
- Warning:The `android.dexOptions.incremental` property is deprecated and it has no effect on the buil
- JSP概述
- pb_ds平衡树-模板
- php写入文件fwrite和file_put_contents函数比较
- 防止网页被嵌入框架的代码
- 获取zip包中的jar。然后将zip包中的jar拷贝在指定目录。个人demo
- centos上安装mysql并设置远程访问
- PHP远程访问文件