【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )

简介: 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )

文章目录

一、属性缺失 propertyMissing 函数回调

二、方法缺失 methodMissing 函数回调

三、完整代码示例





一、属性缺失 propertyMissing 函数回调


在定义类时 , 重写 propertyMissing 方法 ;


如果访问不存在的属性 , 则不会报错 , 而是回调 propertyMissing 方法 ;


def propertyMissing(String name) {
        println "propertyMissing($name)"
        return null
    }
    def propertyMissing(String name, def arg) {
        println "propertyMissing($name, $arg)"
    }



代码示例 :


class Student {
    def propertyMissing(String name) {
        println "propertyMissing($name)"
        return null
    }
    def propertyMissing(String name, def arg) {
        println "propertyMissing($name, $arg)"
    }
}
def student = new Student()
// 访问不存在的属性
// 如果实现了 propertyMissing 方法
// 则不会报错 , 而是回调 propertyMissing 方法
student.age = 19


执行结果 :


propertyMissing(age, 19)






二、方法缺失 methodMissing 函数回调


在定义类时 , 重写 methodMissing 方法 ;


如果调用不存在的方法 , 则不会报错 , 而是回调 methodMissing 方法 ;


def methodMissing(String name, def args) {
        println "methodMissing($name, $args)"
        return null
    }


代码示例 :



class Student {
    def methodMissing(String name, def args) {
        println "methodMissing($name, $args)"
        return null
    }
}
def student = new Student()
// 调用对象不存在的方法
// 如果实现了 methodMissing(String name, def args) 方法
// 则不会报错 , 而是调用上述 methodMissing 方法
student.hello()


执行结果 :


methodMissing(hello, [])






三、完整代码示例


完整代码示例 :



class Student {
    def propertyMissing(String name) {
        println "propertyMissing($name)"
        return null
    }
    def propertyMissing(String name, def arg) {
        println "propertyMissing($name, $arg)"
    }
    def methodMissing(String name, def args) {
        println "methodMissing($name, $args)"
        return null
    }
}
def student = new Student()
// 调用对象不存在的方法
// 如果实现了 methodMissing(String name, def args) 方法
// 则不会报错 , 而是调用上述 methodMissing 方法
student.hello()
// 访问不存在的属性
// 如果实现了 propertyMissing 方法
// 则不会报错 , 而是回调 propertyMissing 方法
student.age = 19


执行结果 :


methodMissing(hello, [])
propertyMissing(age, 19)

image.png

目录
相关文章
【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )
209 0
【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
180 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
119 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
140 0
【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
128 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
130 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
150 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
145 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
132 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
124 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )