我可以使用groovy的默认getter / setter来帮助实现一个java接口吗? [英] Can I use groovy's default getters / setters to help implement a java interface?

查看:541
本文介绍了我可以使用groovy的默认getter / setter来帮助实现一个java接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个导入的库中扩展了一个非常简单的Java接口。界面非常简单,它声明的唯一方法是属性列表的getter和setter。



我的应用程序是用Groovy编写的,所以我想用Groovy类实现这个Java接口。



我的印象是,Groovy默认为其任何类的属性创建了getter和setter - 我可以使用这些默认的getter和setter来满足Java接口的要求吗?



图书馆的Java接口:

$ $ p $ public interface Animal {//接口
public String getName();
public void setName(String name);
public Integer getAge();
public void setAge(Integer age);
}

我希望能够像Groovy一样实现它(但我的编译器抱怨缺少setter):

  public class Cat implements Animal {// Groovy class 
public String name ;
公众整数年龄;


解决方案

你可以用groovy 属性,但要考虑 字段和属性


字段是类或特质的成员其中:


  • 强制访问修饰符(公共,受保护或私有)
  • 一个或多个可选修饰符(静态,最终,同步)

  • 可选类型

  • 必填名称


[...]


属性 是私人领域和getters / setters的组合。
您可以定义一个属性:


  • 缺席访问修饰符(不公开,保护或

  • 一个或多个可选修饰符(static,final,synchronized)
  • 可选类型
  • 一个强制名称



然后Groovy会适当地生成getters / setters。

当你放置一个明确的访问修饰符时,实际上是使用一个字段,所以getters / setters不会生成,这就是为什么编译器抱怨不能有一个摘要方法在一个非抽象类中,因为getters / setters不在那里。


I am extending a very simple Java interface from an imported library. The interface is so simple that the only methods it declares are getters and setters for a list of properties.

My application is written in Groovy, so I'd like to implement this Java interface with a Groovy class.

I was under the impression that Groovy created getters and setters by default for any of its classes' properties - can I use these default getters and setters to satisfy the Java interface's requirements?

Library's Java interface:

public interface Animal {  // java interface
    public String getName();
    public void setName(String name);
    public Integer getAge();
    public void setAge(Integer age);
}

I hoped I'd be able to implement it like this with Groovy (but my compiler is complaining about missing setters):

public class Cat implements Animal { // Groovy class
    public String name;
    public Integer age;
}

解决方案

You can do that with groovy properties, but take into account the difference between fields and properties:

A field is a member of a class or a trait which:

  • a mandatory access modifier (public, protected, or private)
  • one or more optional modifiers (static, final, synchronized)
  • an optional type
  • a mandatory name

[...]

A property is a combination of a private field and getters/setters. You can define a property with:

  • an absent access modifier (no public, protected or final)
  • one or more optional modifiers (static, final, synchronized)
  • an optional type
  • a mandatory name

Groovy will then generate the getters/setters appropriately.

When you put an explicit access modifier, you are actually using a field, so getters/setters are not generated and that's why the compiler complains about Can't have an abstract method in a non-abstract class, since getters/setters are not there.

这篇关于我可以使用groovy的默认getter / setter来帮助实现一个java接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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