当前位置:网站首页>[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边栏推荐
- MySQL服务莫名宕机的解决方案
- Livelocks and deadlocks of concurrency control
- Search: Future Vision (moving sword)
- Official clarification statement of Jihu company
- K210学习笔记(四) K210同时运行多个模型
- A trip to Suzhou during the Dragon Boat Festival holiday
- Nacos 的安装与服务的注册
- Depth first DFS and breadth first BFS -- traversing adjacency tables
- 如何向mongoDB中添加新的字段附代码(全)
- Oracle hint understanding
猜你喜欢

Leetcode simple question: the minimum cost of buying candy at a discount

Calculation method of boundary IOU

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

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

AD637使用笔记

Metaverse Ape上线倒计时,推荐活动火爆进行

Installation of VMware Workstation

科技云报道:算力网络,还需跨越几道坎?

The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
随机推荐
Text组件新增内容通过tag_config设置前景色、背景色
微服务链路风险分析
Character conversion PTA
PyGame practical project: write Snake games with 300 lines of code
[Yugong series] go teaching course in July 2022 004 go code Notes
Meituan dynamic thread pool practice ideas, open source
FBO and RBO disappeared in webgpu
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Ad637 notes d'utilisation
database mirroring
Leetcode simple question: find the nearest point with the same X or Y coordinate
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
Reptile practice
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
Pl/sql basic case
Serializability of concurrent scheduling
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Concurrency control of performance tuning methodology
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Granularity of blocking of concurrency control