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

如何在Interfaces中使用Groovy的属性?

发布时间:2020-12-14 16:23:46 所属栏目:大数据 来源:网络整理
导读:假设我已经在Groovy中定义了一个接口(带有属性),如下所示: public interface GroovyInterface{ int intProperty; boolean boolProperty;} 然后我实现了接口: public class GroovyImplementation implements GroovyInterface{ // How do I implement the pr
假设我已经在Groovy中定义了一个接口(带有属性),如下所示:

public interface GroovyInterface
{       
    int intProperty;       
    boolean boolProperty;
}

然后我实现了接口:

public class GroovyImplementation implements GroovyInterface
{
    // How do I implement the properties here?        
}

我现在如何在具体类中实现属性? @Override不起作用,因为IntelliJ抱怨我无法覆盖字段.
我读过this similar question,但它只说可以在接口中使用属性,但不能如何使用.

解决方法

与在Java中一样,您无法在界面中指定属性.但是使用Groovy,你可以使用一个特性:

trait GroovyInterface
{
    int intProperty
    boolean boolProperty
}

然后,您可以像使用“implements GroovyInterface”的接口一样使用它.

有关特征的更多信息,请咨询http://docs.groovy-lang.org/next/html/documentation/core-traits.html

(编辑:李大同)

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

    推荐文章
      热点阅读