POJO中是否需要吸气剂和吸气剂 [英] Is it necessary to have getters and setters in POJO's

查看:62
本文介绍了POJO中是否需要吸气剂和吸气剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读整洁的代码书,其中指出该类不应公开其数据的内部状态,而应仅公开其行为。如果一个非常简单明了的Java Bean暴露了getter和setter的内部状态,那么仅删除它们并使其私有成员公开是值得的吗?还是只是将类视为数据结构?

I have been going through clean code book which states that the class should not expose the internal state of its data and only should be exposing the behavior. In case of a very simpl and dumb java bean exposing the internal state which getter's and setters, is it not worth just removing them and make the private members public? Or just treat the class as a data structure?

推荐答案

我不这样认为。这取决于对象的生命周期及其暴露(外部修改)。

I don't think so. It depends of the lifetime of your Object and its "exposure" (external modification).

如果仅将其用作数据结构,则以安全的方式公开字段(最后)听起来足够:

If you're only using it as a data structure, exposing fields in secure way (final) sounds enough:

public class Person {
    public final String firstName;
    public final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

这篇关于POJO中是否需要吸气剂和吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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