作者 内容
 donnybaby   关于类之间的通信问题!急!!!!
 

我根据《设计模式》的例子设计了迷宫游戏的程序
其中有
class MapSite { // 迷宫中各个图元的抽象类
public:
virtual bool Enter () const = 0; // 各个图元的enter()行为的接口
};

然后有:
class Door : public MapSite {
public:
//...constructor/destructor and other members here!
virtual bool Enter () const;

private:
bool _isOpen; //表示门是否开着
};

//接着还有Room, Wall ... 都是MapSite的派生类!

class Player {
public:
bool Enter (MapSite *object) const { return object->Enter ();}

private:
bool _hasKey; //表示player是否有钥匙!
};

问题来了,door和room, wall对象在Player::Enter (MapSite *object)里面调用都用一个形式,但是我怎样才能让Door对象知道Player对象是具有钥匙的呢,同时又不破坏Enter()接口的行为一致性?

请ggjj们指教!

谢谢!

 02/12/07 19:46 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 tacone  回复: 关于类之间的通信问题!急!!!!
 

在class Door 中定义一个public方法
bool Open(Player thePlayer)
{
//其间调用thePlayer.GetKey()检查是否有KEY
}
在class Player 中定义一个public方法
bool GetKey()
{
return _GetKey();
}
如何?

 02/12/09 16:28 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 tacone   更正
 


在class Player 中定义一个public方法
bool GetKey()
{
return _hasKey(); //上帖这里写错了
}

 02/12/09 16:50 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 donnybaby   回复: 关于类之间的通信问题!急!!!!
 

让我想想……

谢谢你!
:)

 02/12/09 18:11 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首