公共数据成员与Getters,Setters [英] Public Data members vs Getters, Setters

查看:146
本文介绍了公共数据成员与Getters,Setters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Qt和C ++工作。我有类有私有数据成员和公共成员函数。我有类的数据成员的公共getters和setters。

I am currently working in Qt and so C++. I am having classes that has private data members and public member functions. I have public getters and setters for the data members available in the class.

现在我的问题是,如果我们的类中有数据成员的getters和setter,那么将这些数据成员设为私有的意义是什么? >我同意让基类中的私有数据成员听起来是逻辑的。但除此之外,拥有私人会员等等,他们的吸气者和安装者似乎对我来说是不合逻辑的。

Now my question is, if we have getters and setters for data members in our classes then what's the point in making those data members as private? I agree having private data members in Base classes sounds logical. But besides that, having private members and so do their getters and setters doesn't seem to be of a logical one for me.

或者改为,我们可以将所有变量设为公开,这样就不需要getter和setter了?这是一个很好的做法吗?我知道有私人成员确保数据抽象,但有getters和setters实际上允许访问这些变量很容易。欢迎任何关于此的指针。

Or instead can we make all variables as public so that no need for getters and setters at all? Is it a good practice to have those? I know having private members ensure data abstraction but having getters and setters actually lets access to those variables quite easily. Any pointers regarding this are welcome.

推荐答案

你应该有方法来做事情。如果这些事情之一碰巧对应一个特定的内部变量是伟大的,但没有什么,告诉这个给你的类的用户。

Neither. You should have methods that do things. If one of those things happens to correspond with a specific internal variable that's great but there should be nothing that telegraphs this to the users of your class.

私人数据是私人的你可以随时替换实现(并且可以做完全重建,但这是一个不同的问题)。一旦你把瓶子里的精灵放在瓶子里,你就会发现不可能把它推回去。

Private data is private so you can replace the implementation whenever you wish (and can do full rebuilds but that's a different issue). Once you let the Genie out of the bottle you will find it impossible to push it back in.

编辑:我对另一个回答发表了评论。

Following a comment I made to another answer.

我的观点是,你提出的问题是错误的。没有关于使用getter / setter或具有公共成员的最佳实践。只有什么是最适合你的具体对象,以及它如何建模一些特定的现实世界的东西(或想象的事情,也许在游戏的情况下)。

My point here is that you are asking the wrong question. There is no best practice with regard to using getters/setters or having public members. There is only what is best for your specific object and how it models some specific real world thing (or imaginary thing perhaps in the case of game).

个人getters / setters是两个邪恶中的较小者。因为一旦你开始创建getter / setter,人们就会停止设计对象,关注数据应该是可见的,什么数据不应该。对于公共成员来说,更糟糕的是,因为趋势会使一切都公开。

Personally getters/setters are the lesser of two evils. Because once you start making getters/setters, people stop designing objects with a critical eye toward what data should be visible and what data should not. With public members it is even worse because the tendency becomes to make everything public.

而是检查对象的行为和对象的含义。然后创建为该对象提供自然接口的方法。这种自然界面涉及使用getter和setter暴露一些内部属性。但重要的是,你提前想到了它,并为设计理由创造了getters / setters。

Instead, examine what the object does and what it means for something to be that object. Then create methods that provide a natural interface into that object. It that natural interface involves exposing some internal properties using getters and setters so be it. But the important part is that you thought about it ahead of time and created the getters/setters for a design justified reason.

这篇关于公共数据成员与Getters,Setters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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