简体   繁体   English

java类中的Groovy特性

[英]Groovy traits in java classes

I am working on an existing java codebase but have convinced the team to use cross compilation so new development can be done in groovy, while still using the old codebase. 我正在研究现有的Java代码库,但已经说服团队使用交叉编译,因此可以在groovy中完成新的开发,同时仍然使用旧的代码库。 Best of both worlds, low risk, lots of benefits, etc. 最好的两个世界,低风险,很多好处等。

I have a problem i am trying to solve that is perfectly handled by Groovy's trait feature, but it has to work with the existing java classes or new ones for the dev's that still want to write in java. 我有一个问题,我试图解决这个问题完全由Groovy的特征功能处理,但它必须使用现有的java类或新的仍然想要用java编写的开发人员。

doing class duck implements FlyingAbility { in java throws an error about implementation, and IntelliJ's automatix fix is to implement all of the methods in the trait in the java class, as if it was just an interface. class duck implements FlyingAbility {在java中引发了关于实现的错误,而IntelliJ的automatix修复是在java类中实现trait中的所有方法,就像它只是一个接口一样。

Is there a way to have traits use traits in java classes similarly to how they work in groovy classes? 有没有办法让traits在java类中使用traits,就像它们在groovy类中的工作方式一样?

You can't use traits in Java classes. 您不能在Java类中使用traits。 Groovy traits are implemented as a compile-time transformation of the class they're applied to, and Java classes don't pass through the Groovy compiler. Groovy特性实现为它们应用的类的编译时转换,Java类不通过Groovy编译器。

That said, it's usually very simple to convert Java code to Groovy , and adding @CompileStatic to the class typically generates code that performs about like the equivalent Java, although (for now) with a bit larger .class file. 也就是说, 将Java代码转换为Groovy通常非常简单 ,并且将@CompileStatic添加到类中通常会生成与等效Java类似的代码,尽管(现在)使用更大的.class文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM