getter和setter是不好的面向对象的设计? [英] Getters and Setters are bad OO design?

查看:154
本文介绍了getter和setter是不好的面向对象的设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getter和setter是坏

简单阅读了上面的文章中,我发现,getter和setter方法​​是不好的面向对象设计,因为他们去反对封装和数据隐藏应尽量避免。由于这是怎么能避免创建对象时,如何可以在一个模型对象要考虑到这一点。

Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating objects and how can one model objects to take this into account.

在需要一个getter或setter可使用的其他替代品的情况下?

In cases where a getter or setter is required what other alternatives can be used?

感谢。

推荐答案

由自己或吸气制定者都不会坏的面向对象设计。

Getters or setters by themselves are not bad OO design.

什么是坏的编码习惯,其中包括一个getter和每个成员的setter自动,是否需要或者不是的getter / setter(加上制作成员公开这不应该是公共的) - 因为这基本上暴露类的实现到外界违反了信息隐藏/抽象。有时,这是由IDE,这意味着这样的做法是显著更wides比它希望$ P $垫自动完成的。

What is bad is coding practice which includes a getter AND a setter for EVERY single member automatically, whether that getter/setter is needed or not (coupled with making members public which should not be public) - because this basically exposes class's implementation to outside world violating the information hiding/abstraction. Sometimes this is done automatically by IDE, which means such practice is significantly more widespread than it's hoped for.

这篇关于getter和setter是不好的面向对象的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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