为什么要使用getters和setters/访问器?

Why use getters and setters/accessors?

实际上会有很多人问这个问题....尤其是它成为Coding Style中一部分的时候。

文章出自LBushkin的回答

There are actually many good reasons to consider using accessors rather than directly exposing fields of a class - beyond just the argument of encapsulation and making future changes easier.

Here are the some of the reasons I am aware of:

  • Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.
  • Hiding the internal representation of the property while exposing a property using an alternative representation.
  • Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without affecting existing consumers.
  • Controlling the lifetime and memory management (disposal) semantics of the property - particularly important in non-managed memory environments (like C++ or Objective-C).
  • Providing a debugging interception point for when a property changes at runtime - debugging when and where a property changed to a particular value can be quite difficult without this in some languages.
  • Improved interoperability with libraries that are designed to operate against property getter/setters - Mocking, Serialization, and WPF come to mind.
  • Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods.
  • Allowing the getter/setter to be passed around as lambda expressions rather than values.
  • Getters and setters can allow different access levels - for example the get may be public, but the set could be protected.

简单的翻译:

实际上有许多充分的理由考虑使用访问器而不是直接暴露类的字段 - 除了封装参数和使未来的更改更容易。

以下是我所知道的一些原因:

  • 对属相相关联行为的封装——这让未来添加附加功能更加容易(如验证功能)。
  • 可选择公开属性的方式,同时隐藏属性的内部表现。
  • 让公开的接口免于修改——允许公共接口在实现更改时保持不变,而不会影响现有的使用者。
  • 控制属性的生命周期和内存管理(处置)语义 - 在非托管内存环境(如C ++或Objective-C)中尤为重要。
  • 当属性在runtime中发生改变时,为它提供调试拦截点 - 在某些语言中,在调试属性时候,想定位它在什么时候,什么地方,更改为特定值可能非常困难。
  • 改进了与旨在针对属性获取器/设置器进行操作的库的可操作性 - 例如Mocking,Serialization和WPF。
  • 允许继承者通过重写getter / setter方法来更改属性的行为和语义的语义。
  • 允许getter / setter作为lambda表达式而不是值传递。
  • getter和setter可以允许不同的访问级别 - 例如get可以是公共的,但是set可以受到保护。

转载于:https://www.cnblogs.com/CookieBox/p/Why_Use_Accessors.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一键生成getter、setter和builder是一种自动生成代码的功能,可以帮助开发人员快速创建类的属性、访问方法和构造。 在Java开发中,使用IDE(集成开发环境)或者相关的插件可以实现一键生成getter、setter和builder的功能。例如,使用Eclipse IDE,可以通过以下步骤进行一键生成: 1. 在类中定义需要生成getter、setter的属性。 2. 选中这些属性,右键点击,选择Source -> Generate Getters and Setters。 3. 选择需要生成getter和setter的属性,并点击Generate按钮。 4. Eclipse会自动为选中的属性生成对应的getter和setter方法。 同样地,生成builder模式的过程类似: 1. 在类中定义需要生成builder的属性。 2. 选中这些属性,右键点击,选择Source -> Generate Builder。 3. 在生成的对话框中,可以选择是否包含特定的设置方法,如设置默认值或者验证。 4. 点击Generate按钮,生成builder模式的代码。 这样一键生成getter、setter和builder的过程可以极大地简化代码编写的工作量。它能够自动生成属性的访问方法,提供了良好的封装性和可维护性。同时,builder模式也提供了一种优雅的创建对象的方式,使得代码更加可读性和易于扩展。 然而,需要注意的是,一键生成的代码虽然能够快速生成,但开发人员还是需要对生成的代码进行适当的修改和优化,以适应具体的业务场景和需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值