日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )

發布時間:2025/6/17 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、批量方法委托
  • 二、完整代碼示例





一、批量方法委托



在上一篇博客 【Groovy】MOP 元對象協議與元編程 ( 方法委托 | 正常方法調用 | 方法委托實現 | 代碼示例 ) 中 , 將 StudentManager 對象的方法委托給了其內部的 student1 和 student2 成員 , 在 methodMissing 方法中進行方法委托 , 需要使用 student.respondsTo(name, args) 代碼 , 逐個判斷調用的方法是否在 student1 或 student2 成員中 ; 如果 StudentManager 中定義了很多成員 , 那么就需要逐個進行判定 , 寫起來很繁瑣 ;

下面介紹一種實現方法委托的方式 , 可以優雅的處理上述問題 ;


在 StudentManager 中實現

def delegate(Class... classes)

方法 , 方法接收可變長度的 Class 對象作為參數 ;

首先 , 通過反射 , 創建委托對象 ;

def objects = classes.collect{// 通過反射創建要委托的對象it.newInstance()}

然后 , 通過 HandleMetaClass 注入 methodMissing 方法 ;

// 注入方法需要獲取 org.codehaus.groovy.runtime.HandleMetaClass 對象進行注入StudentManager sm = thissm.metaClass.methodMissing = {String name, def args ->}

再后 , 在 objects 數組中查找哪個對象中包含 name(args) 方法 , 返回該對象賦值給 object 對象 ; 該步驟確保被代理類中有指定的方法 ;

// 在 objects 數組中查找哪個對象中包含 name(args) 方法// 返回該對象賦值給 object 對象def object = objects.find{it.respondsTo(name, args)}

最后 , 如果最終找到的 object 方法不為空 , 則向 StudentManager 中注入相應方法 , 然后調用該注入的方法 ;

if (object) {// 注入 name 方法sm.metaClass."$name" = {object.invokeMethod(name, it)}// 調用剛注入的方法invokeMethod(name, args)}

方法委托實現如下 :

class StudentManager{{// 代碼塊中調用 delegate 方法 , 傳入要委托的類delegate(Student1, Student2)}/*** 實現方法委托* @param classes 可變長度的 Class 對象*/def delegate(Class... classes) {def objects = classes.collect{// 通過反射創建要委托的對象it.newInstance()}// 注入方法需要獲取 org.codehaus.groovy.runtime.HandleMetaClass 對象進行注入StudentManager sm = thissm.metaClass.methodMissing = {String name, def args ->// 在 objects 數組中查找哪個對象中包含 name(args) 方法// 返回該對象賦值給 object 對象def object = objects.find{it.respondsTo(name, args)}if (object) {// 注入 name 方法sm.metaClass."$name" = {object.invokeMethod(name, it)}// 調用剛注入的方法invokeMethod(name, args)}}} }



二、完整代碼示例



完整代碼示例 :

class Student1{def hello1(){println "hello1"} }class Student2{def hello2(){println "hello2"} }class StudentManager{{// 代碼塊中調用 delegate 方法 , 傳入要委托的類delegate(Student1, Student2)}/*** 實現方法委托* @param classes 可變長度的 Class 對象*/def delegate(Class... classes) {def objects = classes.collect{// 通過反射創建要委托的對象it.newInstance()}// 注入方法需要獲取 org.codehaus.groovy.runtime.HandleMetaClass 對象進行注入StudentManager sm = thissm.metaClass.methodMissing = {String name, def args ->// 在 objects 數組中查找哪個對象中包含 name(args) 方法// 返回該對象賦值給 object 對象def object = objects.find{it.respondsTo(name, args)}if (object) {// 注入 name 方法sm.metaClass."$name" = {object.invokeMethod(name, it)}// 調用剛注入的方法invokeMethod(name, args)}}} }def sm = new StudentManager()// 方法委托, 直接通過 StudentManager 對象調用 Student1 中的方法 sm.hello1() // 方法委托, 直接通過 StudentManager 對象調用 Student2 中的方法 sm.hello2()/*方法委托 : 如果調用的某個對象方法沒有定義該對象 , 則可以將該方法委托給內部對象執行*/

執行結果 :

hello1 hello2

總結

以上是生活随笔為你收集整理的【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。