加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

什么是Groovy的MetaClass?

发布时间:2020-12-14 16:37:03 所属栏目:大数据 来源:网络整理
导读:Groovy和其他OO编程语言中Meta-Class的使用是什么? 解决方法 Java中没有Meta类。你可能在想07-09: A MetaClass within Groovy defines the behaviour of any given Groovy or Java class. The MetaClass interface defines two parts. The client API,whic
Groovy和其他OO编程语言中Meta-Class的使用是什么?

解决方法

Java中没有Meta类。你可能在想07-09:

A MetaClass within Groovy defines the
behaviour of any given Groovy or Java
class. The MetaClass interface defines
two parts. The client API,which is
defined via the extend
MetaObjectProtocol interface and the
contract with the Groovy runtime
system. In general the compiler and
Groovy runtime engine interact with
methods on this class whilst MetaClass
clients interact with the method
defined by the MetaObjectProtocol
interface

Groovy MetaClass允许您在运行时将行为和状态分配给Classes,而无需编辑原始源代码,它是原始Class上的一个层。

这是Groovy到extend the Java JDK objects使用的机制。

例:

Object.class.metaClass.explode{-> println "Boom! ${delegate} Exploded!"}
"SomeString".explode();
12345.explode();

输出:

Boom! SomeString Exploded!
Boom! 12345 Exploded!

有关更高级的用法,请阅读:MetaClasses

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读