假设我已经在Groovy中定义了一个接口(带有属性),如下所示:
public interface GroovyInterface
{
int intProperty;
boolean boolProperty;
}
Run Code Online (Sandbox Code Playgroud)
然后我实现了接口:
public class GroovyImplementation implements GroovyInterface
{
// How do I implement the properties here?
}
Run Code Online (Sandbox Code Playgroud)
我现在如何在具体类中实现属性?@Override因为IntelliJ抱怨我不能覆盖一个字段,所以不起作用.我已经读过这个类似的问题,但它只是说可以在接口中使用属性,但不能如何使用.
与在Java中一样,您无法在界面中指定属性.但是使用Groovy,你可以使用一个特性:
trait GroovyInterface
{
int intProperty
boolean boolProperty
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以像使用"implements GroovyInterface"的接口一样使用它.
有关特征的更多信息,请参阅http://docs.groovy-lang.org/next/html/documentation/core-traits.html
| 归档时间: |
|
| 查看次数: |
1128 次 |
| 最近记录: |