您的位置:首页 > 其它

设计模式:Inheritance(继承) 和 Delegation(委托)

2014-06-23 10:27 661 查看
设计模式:Inheritance(继承) 和 Delegation(委托)

今天继续讲Design Pattern的内容。所谓 Design Pattern,翻译过来就是设计模式,是OO语言的一些基本运用。今天介绍Design Pattern中的两个基本概念,Inheritance(继承) 和 Delegation(委托)。

先定义一个位图类:

class CBitmap

{

...

Private:

int height;

public:

int GetBitmapHeight(void) { return Iheight; }

}

所谓Inheritance就是继承,我想学过C++的人都知道什么是继承。以上面的CBitmap为例子,如果我们想生成一个CTexture类(Texture:纹理之意),并且保留CBitmap的功能,比如GetBitmapHeight。可以这么做:

class CTexture : public CBitmap

{

public:

CTexture();

~CTexture();

};

当时还有另外一种方法,并不使用继承,而是把CBitmap当做CTexture的一个成员,这就是Delegation。代码如下:

class CTexture

{

...

private:

CBitmap InternalBitmap;

public:

int GetBitmapHeight(void) { return InternalBitmap.GetBitmapHeight(); }

};

参考:/article/1744563.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: