面向对象编程与封装C++
给出下面的 C++ 示例类,您可以看到可以从调用模块访问 pic1 的所有属性/方法。但只能通过 public 下的声明才能访问 pic2。除了刚才提到的一点之外,作为面向对象编程的初学者,我只是想知道专业人士在现实生活中更喜欢哪种方法?
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System;
ref class myClass
{
PictureBox^ pic2;
public:
PictureBox^ pic1;
void setPic2() {pic2 = gcnew PictureBox;}
void addPic2ToControl(System::Windows::Forms::Form^ x) {x->Controls->Add(pic2);}
void setPic2Image(String^ filePath) {pic2->Image = dynamic_cast<Image^>(gcnew Bitmap(filePath));}
//more functions for to access pic2...
};
Given the below sample class in C++, as you can see one can access all properties/methods for pic1 from the calling module. But only can access to pic2 via those declaration under public. Beside the just mentioned point, as a beginner to object oriented programming, I just wonder which method is preferred by the pro for real life implementation?
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System;
ref class myClass
{
PictureBox^ pic2;
public:
PictureBox^ pic1;
void setPic2() {pic2 = gcnew PictureBox;}
void addPic2ToControl(System::Windows::Forms::Form^ x) {x->Controls->Add(pic2);}
void setPic2Image(String^ filePath) {pic2->Image = dynamic_cast<Image^>(gcnew Bitmap(filePath));}
//more functions for to access pic2...
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++ 中的封装通常是通过将类中的字段标记为
private
或protected
来完成的。但是,您不应该完全依赖此作为保持内容本身真正私密的方式 - 例如,如果这实际上是一个安全问题,那么它根本没有帮助(记住friend
函数。)它更倾向于作为一种“分而治之”程序复杂性并保持代码整洁的方法。这是计算机编程中一个古老的挑战,封装只是程序员多年来开发的许多有效技术之一。在源代码中应该如何使用封装?嗯,很多人会坚持,例如,你的类中永远不应该有公共变量,只有公共方法,并且公共变量应该使用“getter”来模拟/setter”函数。我不一定同意这一点,但我认为可以公平地说,您的类应该将其所做的事情视为公共信息,并将其如何做(及其内部状态)视为私人信息。无论如何,这只是常识,即使您没有进行 OOP。对于 C 程序员来说,如果可以的话,函数内的静态(持久)变量被认为比具有全局作用域的变量更好。
Encapsulation in C++ is usually accomplished by marking fields in your class as
private
orprotected
. However, you shouldn't rely on this totally as a way to keep stuff truly private per se-- if this is actually a security issue, for example, it won't help at all (rememberfriend
functions.) It's more intended as a way to "divide-and-conquer" the program complexity and keep the code clean. This is an age-old challenge in computer programming, and encapsulation is just one of many valid techniques programmers have developed over the years.How should encapsulation be used in your source code? Well a lot of people will insist, for example, that you should never have public variables in your classes, only public methods, and that public variables should be emulated using "getter/setter" functions. I don't necessarily agree with this, but I think it's fair to say that your classes should treat what it does as public information, and how it does it (and its internal state) as private information. This is just common sense anyway, even if you're not doing OOP. Among C programmers, a
static
(persistant) variable within a function is considered better practice than a variable with global scope if you can get away with it.什么是封装?
封装意味着将数据和操作该数据的方法绑定在一个单元中。
如何实现封装?
通过创建像
结构
或类
这样的类型
通常,类内的数据成员变量保存在
private
或protected
访问说明符,对它们的访问是通过public
成员函数提供的。这可以防止程序员无意中修改公开可用的成员数据的无心错误。它提供了一种通过显式公开的函数调用来访问和修改成员数据的方法,因此更加谨慎且不易出错。鉴于上述情况,我相信,保持
pic2
私有并通过成员函数
提供对其的访问似乎是合适的方法What is Encapsulation?
Encapsulation
means binding the data and the methods that operate on that data in a single unit.How do you implement Encapsulation?
By creating a
type
likestructure
or aclass
Usually the data member variables inside a class are kept under
private
orprotected
access specifiers and the access to them is provided throughpublic
member functions. This guards honest mistakes of a programmer of accidentally modifying the member data if available publically. It provides a means of accessing and modifying the member data through explicit publically exposed function calls and hence more deliberate and less error prone.Given the above, I believe, keeping
pic2
private and providing access to it throughmember functions
seems to be the appropriate way通过方法限制对成员的访问的优点之一是您现在可以在中心位置(在类本身内)控制变量的管理。如果稍后您决定在将 pic2 提供给调用者之前对其进行一些处理,您可以在访问方法中执行此操作,而无需在多个位置更改它
稍后如果性能是一个问题,您可以考虑内联方法的选项
One of the pros of restricting access to members through methods is that you now control the management of the variable at a central location (within the class itself). If later you decide to do some processing to pic2 before providing it to the caller you can do that in your access methods without the need to change it at multiple places
Later on if performance is a concern you can consider the option of inlining the methods