当前位置:网站首页>[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
边栏推荐
- Metaverse Ape上线倒计时,推荐活动火爆进行
- A substring with a length of three and different characters in the leetcode simple question
- Blocking protocol for concurrency control
- Serializability of concurrent scheduling
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
- AD637使用笔记
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
- The difference between MVVM and MVC
- Web3为互联网带来了哪些改变?
- U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
猜你喜欢
AD637使用筆記
database mirroring
Reptile practice
Shell script, awk condition judgment and logic comparison &||
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
[Yugong series] go teaching course in July 2022 004 go code Notes
Solutions for unexplained downtime of MySQL services
Serializability of concurrent scheduling
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Technology cloud report: how many hurdles does the computing power network need to cross?
随机推荐
Win11运行cmd提示“请求的操作需要提升”的解决方法
FBO and RBO disappeared in webgpu
Pl/sql basic case
MySQL actual combat 45 lecture learning (I)
Overriding equals() & hashCode() in sub classes … considering super fields
Leetcode simple question check whether all characters appear the same number of times
EBS Oracle 11g cloning steps (single node)
Sub total of Pico development
MySQL服务莫名宕机的解决方案
Implementation technology of recovery
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
Database tuning solution
Promql demo service
IIC bus realizes client device
Text组件新增内容通过tag_config设置前景色、背景色
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Leetcode simple question ring and rod
Blocking protocol for concurrency control