封装的原因 [英] The reason for encapsulation

查看:65
本文介绍了封装的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些相关内容,甚至发现了类似的问题,但并没有真正回答这个问题。对我来说,私有化似乎只会使我的生活变得更加艰难,因为我需要在一个类中找到一个私有变量以在其他地方使用它。那么,如果一切都是公开的,问题将出在哪里?会降低程序本身的速度吗?

I've read some things about this, I even found similar question, but it didn't really answer this. For me it seems that privatizing something only makes my life so much harder when I need to find a private variable in a class to use it elsewhere. So what is would the problem be if everything was public? Would it somehow slow the program itself?

推荐答案

必须考虑代码的可维护性。仅当您是项目中唯一的变量并且您将是唯一维护和使用代码的变量时,才能在解决方案中的所有位置访问所有变量才是好的。如果其他人进入了项目并且做了完全不同的事情,他们将能够访问您的方法/变量并将这些事情设置为意外变量。您应该考虑将其作为OOP设计并像这样设计您的类。

You must consider the maintainability of the code. Accessing all the variables everywhere in your solution is good only if you are the only one in the project and you will be the only one that maintain and use the code. If someone else's entered into project and do completely different things they will be able to access your methods/variables and set the things to unexpected variables. You should think as a OOP design and design your classes like that.

这篇关于封装的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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