封装以及为什么必须使用它? [英] Encapsulation and why we must use it?

查看:96
本文介绍了封装以及为什么必须使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们可以通过Public Member直接访问班级成员,

在通过公共财产访问后,我们将班级成员私有化!

这是封装的概念,但我认为它又要完成工作!!

Why when we can directly access the class members with Public Member,

we private the members of class, after access to it through, with a Public Property!!

this is concept of Encapsulation but i think it is done again a work!!

推荐答案

这似乎很愚蠢,但是它的作用是定义您的类与接口之间的接口.外部世界.

如果直接将calls成员定义为Public,则会发生两件事:
1)任何人都可以对其进行读写
2)您无法更改.

第一个可能很重要:如果您拥有该成员的私有身份,并且可以通过公共(或受保护的或内部的)财产提供访问权限,那么您可以拥有一个公共获取者和一个私有设置者:外部世界可以自由访问您提供的信息,但无法更改.这可以帮助保留您的数据完整性,因为它们不能调整"数据的一部分并使代码执行奇怪的事情!

第二个甚至更重要:如果您向外界公开内部字段,那么对代码进行的任何更改 必须使这些字段保持相同 .您无法更改代码的工作方式.举例来说,您从一个SQLCE数据库开始,并将命令公开.您的程序可以正常工作,并且您的课程也可以使用.如此之多,以至于SQLCE不再足够了:切换到SQL Server.您需要做的就是将SqlCeCommand更改为SqlCommand.完毕!测试您的课程,一切都完美.除了,突然没有其他人的代码可以编译...

使用属性可以封装我们的数据,并在不揭示内部工作原理的情况下,揭示关于类的逻辑上必要的内容.更安全,更清洁,更可靠!
It seems silly, but what it does is define the interface between your class and the outside world.

If you define your calls member directly as Public, then two things happen:
1) Anyone can read or write to them
2) You can''t change them.

The first can be important: If you have the member private, and provide access via a public (or protected, or internal) property, then you can have a public getter, and a private setter: the outside world is at liberty to access the info you provide, but it cannot change it. This can help preserve your data integrity, because they can''t "tweak" bits of your data and make your code do strange things!

The second is even more important: If you reveal your internal fields to the outside world, then whatever changes you make to your code must keep those fields the same. You cannot change the way your code works. Say for example, you start with an SQLCE database, and make the command public. Your program works, and your class gets used. So much so, that SQLCE if no longer sufficient: switch to SQL Server. All you need to do is change from SqlCeCommand to SqlCommand. Done! Test your class, everything is perfect. Except, suddenly nobody else''s code compiles...

Properties allow use to encapsulate our data, and reveal what is logically necessary about our classes without revealing the inner workings. Safer, cleaner, and much more reliable!


不,这不是不是封装的概念,只是可访问性.

封装是这样的: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) [ ^ ].

您在上面引用的文章中仅提及了称为信息隐藏机制"的方面.基本上,我们隐藏成员是因为我们是人类,如果我们不保护自己,他们很容易忘记和弄乱事物.

(顺便说一句,访问修饰符并不能真正阻止访问.通过反射,所有私有成员都是可以完全访问的.因此,我们确实对自己隐藏了成员,而不对入侵者隐藏!)

—SA
No, this is not a concept of encapsulation, only the accessibility.

Encapsulation is this: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)[^].

You mention only the aspect called "information hiding mechanism" in the article referenced above. Basically, we hide members because we''re human being who can easily forget and mess-up things if we do not protect themselves.

(By the way, access modifiers does not really prevent access. With Reflection, all private members are perfectly accessible. So we really hide members from ourselves, not from intruders!)

—SA


这篇关于封装以及为什么必须使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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