Say I have an Image
class which performs manipulation on an image file, such as make the image black and white, gray-scale, etc. The image bytes are stored in an attribute which is an array. Now I want other programmers to add functionality to the Image
class if they like to. I assume that this is done by creating their own class which inherits from Image
. Examples of functionality that they could add would be rotate image, crop image, etc.
假设我有一个Image类,它对图像文件执行操作,例如使图像变为黑白,灰度等。图像字节存储在作为数组的属性中。现在我希望其他程序员可以根据需要为Image类添加功能。我假设这是通过创建自己继承自Image的类来完成的。他们可以添加的功能示例包括旋转图像,裁剪图像等。
My question is what should I do in this case to allow other programmers to add functionality to the Image
class, should I make the array that holds the image bytes protected
, which will allow only child classes to modify it directly?
我的问题是,在这种情况下我应该做些什么来允许其他程序员向Image类添加功能,我是否应该保持图像字节受保护的数组,这将只允许子类直接修改它?
2 个解决方案
#1
0
Using protected attribute on the array and a public inheritance access specifier is best. This allows subclasses to keep the same public interface without having to expose too much implementation detail, and still allows the subclass the ability to modify the array.
在数组上使用protected属性和公共继承访问说明符是最好的。这允许子类保持相同的公共接口,而不必暴露太多的实现细节,并且仍允许子类修改数组。
Just as you suggested, that's what the access specifiers are for.
正如您所建议的那样,这就是访问说明符的用途。
http://www.cplusplus.com/doc/tutorial/inheritance/
http://en.wikibooks.org/wiki/C++_Programming/Classes/Inheritance
#2
0
The first thing is to consider whether there are any invariants that your class needs to maintain - for example, that the image member's dimensions aren't change by a derived class because separate x
/y
data members in the base class could get out of sync, or that a pointer to the data is not re-seated by the derived class because some other private member stores pointers into the data to track say brightest-pixel locations or whatever. So, there's a process of deciding what kind of functionality belongs exclusively in your class, and what access you can grant to derived classes without it being easy to break your functionality. You may still have to "trust" derived classes not to do weird things, but you should try to make it hard to do accidentally, and document your base class well. If you want to learn about this kind of thing, you might reasonably start with the Liskov Substitution Principle.
第一件事是考虑是否存在您的类需要维护的任何不变量 - 例如,图像成员的维度不会被派生类更改,因为基类中的单独x / y数据成员可能不同步或者,派生类不会重新定位指向数据的指针,因为某些其他私有成员存储指向数据的指针以跟踪最亮像素位置或其他任何内容。因此,有一个过程可以决定哪些类型的功能专属于您的类,以及您可以授予派生类的访问权限,而不会轻易破坏您的功能。您可能仍然必须“信任”派生类不要做奇怪的事情,但是您应该尝试使其难以做到,并且很好地记录您的基类。如果你想了解这类事情,你可以合理地从Liskov替代原则开始。
#1
0
Using protected attribute on the array and a public inheritance access specifier is best. This allows subclasses to keep the same public interface without having to expose too much implementation detail, and still allows the subclass the ability to modify the array.
在数组上使用protected属性和公共继承访问说明符是最好的。这允许子类保持相同的公共接口,而不必暴露太多的实现细节,并且仍允许子类修改数组。
Just as you suggested, that's what the access specifiers are for.
正如您所建议的那样,这就是访问说明符的用途。
http://www.cplusplus.com/doc/tutorial/inheritance/
http://en.wikibooks.org/wiki/C++_Programming/Classes/Inheritance
#2
0
The first thing is to consider whether there are any invariants that your class needs to maintain - for example, that the image member's dimensions aren't change by a derived class because separate x
/y
data members in the base class could get out of sync, or that a pointer to the data is not re-seated by the derived class because some other private member stores pointers into the data to track say brightest-pixel locations or whatever. So, there's a process of deciding what kind of functionality belongs exclusively in your class, and what access you can grant to derived classes without it being easy to break your functionality. You may still have to "trust" derived classes not to do weird things, but you should try to make it hard to do accidentally, and document your base class well. If you want to learn about this kind of thing, you might reasonably start with the Liskov Substitution Principle.
第一件事是考虑是否存在您的类需要维护的任何不变量 - 例如,图像成员的维度不会被派生类更改,因为基类中的单独x / y数据成员可能不同步或者,派生类不会重新定位指向数据的指针,因为某些其他私有成员存储指向数据的指针以跟踪最亮像素位置或其他任何内容。因此,有一个过程可以决定哪些类型的功能专属于您的类,以及您可以授予派生类的访问权限,而不会轻易破坏您的功能。您可能仍然必须“信任”派生类不要做奇怪的事情,但是您应该尝试使其难以做到,并且很好地记录您的基类。如果你想了解这类事情,你可以合理地从Liskov替代原则开始。