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可以受到保护。

为什么要使用getters和setters/访问器?的更多相关文章

  1. JavaScript中的Get和Set访问器

    今天要和大家分享的是JavaScript中的Get和Set访问器,和C#中的访问器非常相似. 标准的Get和Set访问器的实现   function Field(val){       this.va ...

  2. 【外文翻译】 为什么我要写 getters 和setters

    原文作者: Shamik Mitra 原文链接:https://dzone.com/articles/why-should-i-write-getters-and-setters 当我开始我的java ...

  3. JavaScript 数据属性和访问器属性

    在JavaScript中对象被定义为"无序属性的集合,其属性可以包含基本值.对象或函数."通俗点讲,我们可以把对象理解为一组一组的名值对,其中值可以是数据或函数. 创建自定义对象通 ...

  4. JavaScript 属性类型(数据属性和访问器属性)

    数据属性 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性有 4 个描述其行为的特性. [[Configurable]]:表示能否通过 delete 删除属性从而重新定义属性,能否修 ...

  5. ECMAScript 5中的数据属性和访问器属性

    简介 ECMAScript 定义的对象中有两种特殊的属性, 这两种特殊的属性在你定义对象属性时就会赋予, 我们在必要时可以改写这两种特殊的属性让其属性的访问更加的合理化, 这两种特殊的属性称呼及作用如 ...

  6. JavaScript数据属性与访问器属性

    ES5中对象的属性可以分为‘数据属性’和‘访问器属性’两种. 数据属性一般用于存储数据数值,访问器属性对应的是set/get操作,不能直接存储数据值. 数据属性特性:value.writable.en ...

  7. JS之访问器

    1.在对象中定义get,set访问器属性 <script> var test = { _name:"pmx", _age:18, _born:1990, get nam ...

  8. 反编译ILSpy 无法显式调用运算符或访问器 错误处理方法 转

    反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下,发现问题是在调用属性的时候,都 变成了方法,例如:pivotPoint.set_X(0 ...

  9. 14.C#属性访问器、命名空间、pragma指令(七章7.3-7.5)

    看到一些零星的知识片,今天就用自己的理解说明下,也是因为太简单了,一下就过的,也是我们日常开发中常用.留下一个脚印,当书不在手上的,也能翻出来看看.说下属性访问器.命名空间和pragma指令. 属性访 ...

随机推荐

  1. yii2在控制器中调用另一个控制器方法

    Yii::$app->runAction(控制器/方法名);

  2. uploadify多文件上传实例--C#

    下载uploadify文件 http://www.uploadify.com/ HTML(视图) <html lang="zh-cn"> <head> &l ...

  3. go实现dgraph的各种操作

    go实现dgraph的各种操作 import "github.com/dgraph-io/dgo" import "github.com/dgraph-io/dgo/pr ...

  4. c++析构函数调用时机

  5. JGUI源码:开发中遇到的问题(11)

    1.IE8下浏览器下css body边缘要留一个像素,如果不留的话,很有可能看不到最边缘的像素. 2.同一种颜色在深色背景和浅色背景下给人的感觉不一样,在深色背景下,给人感觉特别亮,所以深色背景下的颜 ...

  6. Redux中间件组合方法

    中间件 https://redux.js.org/glossary#middleware 中间件在发送端 action端点 和 处理方reducer的端点之间. 主要负责将async action转换 ...

  7. 20175315 实验二《Java面向对象程序设计》实验报告

    20175315 实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 1.初步掌握单元测试和TDD 单元测试 任务一:三种代码 用程序解决问题时,要学会写以下三种代码: 伪代码 ...

  8. CodeForces 671C - Ultimate Weirdness of an Array

    题意: 给以一个定义, F(l, r) 的值表示序列 A[1:n]的子序列 A[1....(l-1),(r+1)...n] 之中 任意两个数的最大公约数的最大值. 求 Sum=∑i=1N∑j=1N(F ...

  9. Java定时清理过期文件

    项目中经常需要自动定时去清理一些过期文件,这个其实Java实现挺简单的,核心部分就2个,一个定时任务,一个递归删除文件,不过前提是你的文件放在以“2018-12-05”这样命名的文件夹下,下面直接上核 ...

  10. js 对象,数组,字符串,相互转换

    1:对象转换数组 let obj = {'val1':1, 'val2':2, 'val3':3, 'val4':4}; var arr = [] for (let i in obj) { //取键 ...