为什么要使用getters和setters/访问器?
Why use getters and setters/accessors?
实际上会有很多人问这个问题....尤其是它成为Coding Style中一部分的时候。
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/访问器?的更多相关文章
- JavaScript中的Get和Set访问器
今天要和大家分享的是JavaScript中的Get和Set访问器,和C#中的访问器非常相似. 标准的Get和Set访问器的实现 function Field(val){ this.va ...
- 【外文翻译】 为什么我要写 getters 和setters
原文作者: Shamik Mitra 原文链接:https://dzone.com/articles/why-should-i-write-getters-and-setters 当我开始我的java ...
- JavaScript 数据属性和访问器属性
在JavaScript中对象被定义为"无序属性的集合,其属性可以包含基本值.对象或函数."通俗点讲,我们可以把对象理解为一组一组的名值对,其中值可以是数据或函数. 创建自定义对象通 ...
- JavaScript 属性类型(数据属性和访问器属性)
数据属性 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性有 4 个描述其行为的特性. [[Configurable]]:表示能否通过 delete 删除属性从而重新定义属性,能否修 ...
- ECMAScript 5中的数据属性和访问器属性
简介 ECMAScript 定义的对象中有两种特殊的属性, 这两种特殊的属性在你定义对象属性时就会赋予, 我们在必要时可以改写这两种特殊的属性让其属性的访问更加的合理化, 这两种特殊的属性称呼及作用如 ...
- JavaScript数据属性与访问器属性
ES5中对象的属性可以分为‘数据属性’和‘访问器属性’两种. 数据属性一般用于存储数据数值,访问器属性对应的是set/get操作,不能直接存储数据值. 数据属性特性:value.writable.en ...
- JS之访问器
1.在对象中定义get,set访问器属性 <script> var test = { _name:"pmx", _age:18, _born:1990, get nam ...
- 反编译ILSpy 无法显式调用运算符或访问器 错误处理方法 转
反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下,发现问题是在调用属性的时候,都 变成了方法,例如:pivotPoint.set_X(0 ...
- 14.C#属性访问器、命名空间、pragma指令(七章7.3-7.5)
看到一些零星的知识片,今天就用自己的理解说明下,也是因为太简单了,一下就过的,也是我们日常开发中常用.留下一个脚印,当书不在手上的,也能翻出来看看.说下属性访问器.命名空间和pragma指令. 属性访 ...
随机推荐
- centos7下使用docker安装gitlab
环境背景: Docker化已经成为一种热门,记录一下使用docker引擎安装gitlab的过程. 测试环境: 系统 软件 依赖 CentOS 7.4 GitLab(latest) docker-ce ...
- JAVA helloworld!
idea创建java项目 https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html 本地文档运行 java helloworld ...
- docker时间与系统时间同步的问题
系统是CentOS7,因为开发环境是windows没有这个问题,发布到docker以后当前时间进行比较,发现docker里面用java获取当时间不对,然后查docker的时间少了8个小时. 网上查了很 ...
- 那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)
一. 背景 在刚接触开发的头几年里,说实话,根本不考虑多线程的这个问题,貌似那时候脑子里也有没有多线程的这个概念,所有的业务都是一个线程来处理,不考虑性能问题,当然也没有考虑多线程操作一条记录存在的并 ...
- 《JAVA并发编程实战》示例程序第一、二章
第一章:简介 程序清单1-1非线程安全的数值序列生成器 import net.jcip.annotations.NotThreadSafe; @NotThreadSafe public class U ...
- python3 练手实例5 做一个简单电子时钟
import time,sys,os while(1): t = time.strftime('%Y-%m-%d\n%H:%M:%S',time.localtime(time.time())) pri ...
- echarts笔记
常见问题: 1.x轴和y轴type同时为category时不可行 只能改变方式显示,返回不同名称,如加上百分比显示 formatter:"value%"; var waterLev ...
- Nodejs一键实现微信内打开网页url自动跳转外部浏览器访问的功能
前言 现如今微信对第三方推广链接的审核是越来越严格了,域名在微信中分享转发经常会被拦截,一旦被拦截用户就只能复制链接手动打开浏览器粘贴才能访问,要不然就是换个域名再推,周而复始.无论是哪一种情况都会面 ...
- window C/C++ 简单的IDE编译器
C-Free 官网链接: http://www.programarts.com/cfree_ch/download.htm
- verdi\debussy的使用技巧
verdi\debussy的使用技巧 转载from 大西瓜FPGA 大西瓜FPGA-->https://daxiguafpga.taobao.com fsdb display Debussy本身 ...