需求
游戏项目先确定一个框架,把基础的执行顺序写死,不允许也不需要改动,就可以把顺序写在抽象类里,后面添加怪物和物品只需要继承即可!!!
代码
#include <iostream>
using namespace std;
class Sprite {
protected:
void show() {
cout << name<<"出现了!" << endl;
}
void dispear() {
cout << name<<"消失了!" << endl;
}
void setup() {
cout << name<<"属性设置!" << endl;
}
public:
Sprite(string _name):name(_name){}
void run() {
this->setup();
this->show();
this->hook();
this->dispear();
}
protected:
string name;
virtual void activate() = 0;
virtual void deactivate() = 0;
virtual void hook() = 0;
};
class Monster : public Sprite {
public:
Monster(string _name):Sprite(_name){}
protected:
void activate() {
cout << name<<"激活了!" << endl;
}
void deactivate() {
cout << name<<"怪物消灭了!" << endl;
}
void hook() {
cout << name<<"怪物被技能打到了!" << endl;
}
};
class Player : public Sprite {
public:
Player(string _name) :Sprite(_name) {}
protected:
void activate() {
cout << name<<"激活了!" << endl;
}
void deactivate() {
cout << name<<"死了!" << endl;
}
void hook() {
cout << name<<"放出了技能!" << endl;
}
};
int main(void) {
Monster* m = new Monster("金色狮鹫");
m->run();
cout << "-------------------------------------" << endl;
Player* p = new Player("黄泉");
p->run();
delete m;
delete p;
return 0;
}
结果
金色狮鹫属性设置!
金色狮鹫出现了!
金色狮鹫怪物被技能打到了!
金色狮鹫消失了!
-------------------------------------
黄泉属性设置!
黄泉出现了!
黄泉放出了技能!
黄泉消失了!