Groovy对象和Java对象在Groovy代码中的功能有什么区别? [英] What is the difference between a Groovy object and a Java object with regards to functionality in Groovy code?
问题描述
m尝试一些Java / Groovy(试图找到使Java中的编码更快/更容易的方法),并且实例化这个对象Person,这是一个Java对象,Person类与Groovy没有任何关系。没有实现GroovyObject或任何东西)。 Person有一个受保护的字段,名称和getter / setters。我能够在Groovy测试用例(扩展GroovyTestCase)中访问它,这是一个不同的包,不应该像这样访问:
Person person = new Person('joe')
printlnName:$ {person.name}
是不是使用Groovy对象会有的元信息,即Groovy会拦截,获取person.name的名称部分并调用getName()方法或类似的东西?但是人是一个Java对象......即它不应该有可用的逻辑。
我没有实现GroovyObject或扩展的GroovyObjectSupport,但它看起来像这个Java对象有Groovy功能我可以使用。
我的问题是,我可以从Groovy中使用的Java对象中假设什么?显然有一些事情在后台完成,用一些Groovy特性来扩充Java定义的类......但是,哪些特性?
所以,我想我的问题归结为:在Groovy环境中使用Java类时做了什么?
有关该主题的任何信息将不胜感激,谢谢:)
Groovy确实增强了您的Java类。问题在于何时想要使用Java中的这些增强型Java类。然后,您需要为 GroovyObject
和 GroovyObjectSupport
策略。
关于你所期待的,你可以查看groovy文档: codehaus.org/groovy-jdk/rel =nofollow> groovy-jdk 显示了对JDK的Groovy增强功能
<
Java对象通过Groovy的元对象进行了增强协议(MOP),拦截并自行调度方法和属性。这就是为什么你可以拦截东西,这就是为Java的最终类添加新方法的原因。
Groovy的MOP使用缓存策略和反射器类来实现良好的性能。 / p>
First of all, excuse me if I say something obvious because I did not research the answer to this question, it's just something that came to mind and couldn't find an answer to with a quick google search (I also don't know Groovy well).
I'm reading Groovy in action (1st ed) and in section 7.6 it states that "If you want a usual Java class to be recognized as a Groovy class, you only have to implement the GroovyObject interface. For convenience, you can also subclass the abstract class GroovyObjectSupport, which provides default implementations."
So I'm trying out some Java/Groovy (trying to find ways to make coding in Java faster/easier), and I'm instantiating this object Person, which is a Java object and the Person class does not have anything to do with Groovy (i.e. doesn't implement GroovyObject or anything). Person has a protected field, name, with getters/setters. I am able to access this like so in a groovy test case (extends GroovyTestCase), which btw is in a different package and should not be accessible like this:
Person person = new Person('joe')
println "Name: ${person.name}"
Isn't that using the meta info which a Groovy object would have i.e. Groovy would intercept, get the "name" part of "person.name" and call the getName() method or something like that? But person is a Java object... i.e. it shouldn't have that logic available.
I have not implemented GroovyObject or extended GroovyObjectSupport, yet it seems like this Java object has Groovy features I can use.
My question is, what can I assume from Java objects used in Groovy? There is obviously something being done behind the scenes to augment Java defined classes with some Groovy features... but, which features?
So, I guess my question boils down to: "What is done to Java classes when used within a Groovy context?"
Any info regarding the subject would be appreciated, thanks :)
Groovy indeed enhances your Java classes. The problem is when you want to use these enhanced Java classes from Java. Then you need to go for the GroovyObject
and GroovyObjectSupport
strategy.
About what you can expect, you can check the groovy docs:
- the groovy-jdk shows the Groovy enhancements to the JDK
- the groovy-api shows the stuff specific from the Groovy library
Also the eclipse plugin works great in autocompleting your Java classes from Groovy.
Java objects are enhanced through Groovy's Meta Object Protocol (MOP), which intercepts and makes its own dispatch to methods and attributes. That is why you can intercept stuff and that's how new methods are added to Java's final classes.
The Groovy's MOP uses cached strategy and reflector classes to achieve a good performance.
这篇关于Groovy对象和Java对象在Groovy代码中的功能有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!