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

java – 从Play中生成的getter和setter获得好处!骨架

发布时间:2020-12-14 05:38:01 所属栏目:Java 来源:网络整理
导读:运行时模型类的每个公共场的 Play! framework generates getters and setters. public class Product { public String name; public Integer price;} 将转变为 public class Product { public String name; public Integer price; public String getName() {
运行时模型类的每个公共场的 Play! framework generates getters and setters.
public class Product {

    public String name;
    public Integer price;
}

将转变为

public class Product {

    public String name;
    public Integer price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}

手册进一步解释:

那么当你想访问一个属性时,你可以写:

product.name = "My product";
product.price = 58;

哪个在加载时转换为:

product.setName("My product");
product.setPrice(58);

…并警告:

You can’t directly use getter and setter methods to access properties
if you rely on automatic generation. These methods are generated at
runtime. So if you reference them in code you write,the compiler
won’t find the methods and will generate an error.

因为我不能从Play外面使用这些getter和setter!项目,我看不出有什么好处.与所有现代IDE的考虑相比,公共领域,重构(封装一个领域和更改呼叫者)有什么好处?

解决方法

简答:豆需要它们.

更长:豆类规格要求(除其他外)每个内部字段的吸气剂/设定器.我不是100%肯定的,但我认为Hibernate和Groovy模板都希望Java Bean(POJO Bean,而不是Java EE),因此他们会要求getter / setter.播放只是节省你的时间,所以你不用担心锅炉版代码(除非你想为某种原因定义你自己的吸气剂/设定器,你可以做).

(编辑:李大同)

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

    推荐文章
      热点阅读