Java类中的Groovy特性 [英] Groovy traits in java classes

查看:89
本文介绍了Java类中的Groovy特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究现有的Java代码库,但已经说服了团队使用交叉编译,以便可以在仍旧使用旧代码库的情况下以常规方式进行新开发.两全其美,风险低,收益多,等等.

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. Best of both worlds, low risk, lots of benefits, etc.

我要解决的一个问题是由Groovy的trait特性完全解决的,但是它必须与现有的Java类或仍要用Java编写的开发人员一起使用的新类.

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.

在Java中执行class duck implements FlyingAbility {会引发有关实现的错误,而IntelliJ的automatix修复程序是在java类的trait中实现所有方法,就好像它只是一个接口一样.

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.

是否有一种方法可以让特质在Java类中使用特质,就像它们在groovy类中的工作方式一样?

Is there a way to have traits use traits in java classes similarly to how they work in groovy classes?

推荐答案

您不能在Java类中使用特征. Groovy特性是作为对其应用的类的编译时转换而实现的,而Java类不会通过Groovy编译器传递.

You can't use traits in Java classes. 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.

也就是说,将Java代码转换为Groovy通常很简单,并添加了该类的@CompileStatic通常会生成与Java等效的代码,尽管(现在)具有更大的.class文件.

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特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆