当前位置:网站首页>[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
2022-07-05 22:17:00 【Programmer community】
One 、 adopt MetaMethod#invoke perform Groovy Method
Already defined Groovy class Student , And create this kind of object ;
class Student {
def name; def hello() {
println "Hello ${name}" }}def student = new Student(name: "Tom")
adopt MetaMethod#invoke perform Groovy Method :
First , obtain Groovy Object's MetaClass ,
student.getMetaClass()
then , call MetaClass#getMetaMethod Method obtain MetaMethod object ,
MetaMethod metaMethod = student.getMetaClass().getMetaMethod("name", null)
Last , call MetaMethod#invoke Method , Execute the acquired MetaMethod Corresponding Groovy Method ;
metaMethod.invoke(student, null)
Two 、 Complete code example
Complete code example :
class Student {
def name; def hello() {
println "Hello ${name}" }}def student = new Student(name: "Tom")// Call directly hello Method student.hello()// adopt GroovyObject#invokeMethod call hello Method // The second parameter is the function parameter , If void Then in nullstudent.invokeMethod("hello", null)// obtain Metamethod MetaMethod metaMethod = student.getMetaClass().getMetaMethod("name", null)// Execution meta method metaMethod.invoke(student, null)
Execution results :
Hello TomHello TomHello Tom
边栏推荐
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- Pl/sql basic syntax
- 极狐公司官方澄清声明
- Evolution of large website architecture and knowledge system
- Promql demo service
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- The simple problem of leetcode is to split a string into several groups of length K
- What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
猜你喜欢
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
PyGame practical project: write Snake games with 300 lines of code
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Livelocks and deadlocks of concurrency control
ESP32 hosted
Leetcode simple question check whether all characters appear the same number of times
Recovery technology with checkpoints
How to use tensorflow2 for cat and dog classification and recognition
Metaverse Ape上线倒计时,推荐活动火爆进行
Installation of VMware Workstation
随机推荐
Daily question brushing record (XIV)
Leetcode simple question: find the nearest point with the same X or Y coordinate
A trip to Suzhou during the Dragon Boat Festival holiday
Pinctrl subsystem and GPIO subsystem
Leetcode simple question: the minimum cost of buying candy at a discount
boundary IoU 的计算方式
Leetcode simple question ring and rod
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
Meituan dynamic thread pool practice ideas, open source
数据泄露怎么办?'华生·K'7招消灭安全威胁
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
"Chris Richardson microservices series" uses API gateway to build microservices
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
Index optimization of performance tuning methodology
Ad637 notes d'utilisation
Hcip day 16
Matlab draws a cute fat doll
Some tutorials install the database on ubantu so as not to occupy computer memory?