set / get方法在C ++中 [英] set/get methods in C++

查看:202
本文介绍了set / get方法在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java程序员和API似乎有利于显式的set / get方法。

Java programmers and API seems to favor explicit set/get methods.

但是我得到的印象是C ++社区对这样的做法。
如果是这样,是否有特殊的原因(除了更多的代码行)为什么会是这样?

however I got the impression C++ community frowns upon such practice. If it is so,is there a particular reason (besides more lines of code) why this is so?

另一方面,选择使用方法而不是直接访问?

on the other hand, why does Java community choose to use methods rather than direct access?

谢谢

推荐答案

一个设计良好的类最好不要有太多的get和set。在我看来,太多的获取和集合基本上表明一个事实,其他人(和可能的许多人)需要我的数据来实现他们的目的。在这种情况下,为什么这些数据属于我在第一位?这违反了封装的基本原理(数据+操作在一个逻辑单元中)。

A well designed class should ideally not have too many gets and sets. In my opinion, too many gets and sets are basically an indication of the fact that someone else (and potentially many of them) need my data to achieve their purpose. In that case, why does that data belong to me in the first place? This violates the basic principle of encapsulation (data + operations in one logical unit).

因此,虽然没有技术限制,和'get'方法,如果你的类接口中太多的get和set在系统中太多的其他实体使用,你应该暂停和重新检查你的设计。

So, while there is no technical restriction and (in fact abundance of) 'set' and 'get' methods, I would say that you should pause and reinspect your design if you want too many of those 'get' and 'set' in your class interface used by too many other entities in your system.

这篇关于set / get方法在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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