当前位置:网站首页>[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
边栏推荐
- Blocking of concurrency control
- Damn, window in ie open()
- Shell script, awk condition judgment and logic comparison &||
- database mirroring
- Serializability of concurrent scheduling
- MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
- 700. Search in a Binary Search Tree. Sol
- Regular expressions and re Libraries
- 微服务链路风险分析
- How to develop and introduce applet plug-ins
猜你喜欢
Concurrency control of performance tuning methodology
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
[Yugong series] go teaching course in July 2022 004 go code Notes
Granularity of blocking of concurrency control
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Common interview questions of redis factory
A trip to Suzhou during the Dragon Boat Festival holiday
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
2022软件测试工程师涨薪攻略,3年如何达到30K
Decorator learning 01
随机推荐
Overview of database recovery
"Chris Richardson microservices series" uses API gateway to build microservices
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Ad637 notes d'utilisation
FBO and RBO disappeared in webgpu
Installation of VMware Workstation
Decorator learning 01
AD637 usage notes
Pinctrl subsystem and GPIO subsystem
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Draw a red lantern with MATLAB
50. Pow(x, n). O(logN) Sol
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Basic grammar of interview (Part 1)
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
FBO and RBO disappeared in webgpu
如何向mongoDB中添加新的字段附代码(全)
Win11运行cmd提示“请求的操作需要提升”的解决方法
Business learning of mall order module
Nacos 的安装与服务的注册