当前位置:网站首页>[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边栏推荐
- What changes has Web3 brought to the Internet?
- MySQL服务莫名宕机的解决方案
- Overriding equals() & hashCode() in sub classes … considering super fields
- Performance monitoring of database tuning solutions
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Metaverse Ape获Negentropy Capital种子轮融资350万美元
- Blocking protocol for concurrency control
- Business learning of mall commodity module
- Reptile practice
- Server optimization of performance tuning methodology
猜你喜欢

Win11运行cmd提示“请求的操作需要提升”的解决方法

装饰器学习01

Bitbucket installation configuration

Depth first DFS and breadth first BFS -- traversing adjacency tables

Promql demo service

Search: Future Vision (moving sword)
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/56/d596e7c7bec9abd888e8f18f9769f8.png)
[Yugong series] go teaching course in July 2022 004 go code Notes

Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award

Two stage locking protocol for concurrency control

How to view Apache log4j 2 remote code execution vulnerability?
随机推荐
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
Character conversion PTA
700. Search in a Binary Search Tree. Sol
Sentinel production environment practice (I)
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Granularity of blocking of concurrency control
Sparse array [matrix]
Summary of concurrency control
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Nacos 的安装与服务的注册
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Common interview questions of JVM manufacturers
[agc009e] eternal average - conclusion, DP
Blocking of concurrency control
Win11运行cmd提示“请求的操作需要提升”的解决方法
Kubernetes Administrator certification (CKA) exam notes (IV)
实战:fabric 用户证书吊销操作流程
如何向mongoDB中添加新的字段附代码(全)
Performance testing of software testing
A substring with a length of three and different characters in the leetcode simple question