抽象类Monster
时间: 2023-12-23 19:25:19 浏览: 31
抽象类Monster是一个包含了一个抽象方法attack和私有属性name的抽象类。由于它是一个抽象类,因此不能被实例化,只能被继承。子类需要提供attack方法的具体实现。以下是一个抽象类Monster的示例代码:
```python
from abc import ABC, abstractmethod
class Monster(ABC):
def __init__(self, name):
self._name = name
@abstractmethod
def attack(self):
pass
def get_name(self):
return self._name
```
在这个示例中,我们使用了Python内置的abc模块来定义抽象类。抽象类中的attack方法被标记为abstractmethod,这意味着子类必须提供其具体实现。私有属性name被定义为一个下划线开头的变量,这意味着它只能在类内部访问。get_name方法用于获取name属性的值。
相关问题
任务描述 本关任务:编写一个能实现游戏角色变身的小程序。 相关知识 为了完成本关任务,你需要掌握:1.如何定义抽象类(接口),2.如何使用抽象类。 题目内容 已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。要求,使用抽象类及的基类指针实现。 编程要求 根据提示,在右侧编辑器补充代码,完成程序。 测试说明 平台会对你编写的代码进行测试: 测试输入: 1 预期输出: hero is idleling... hero is attack... hero is running... hero is dead... 测试输入: 2 预期输出: Monster is idleling... Monster is attack... Monster is running... Monster is dead... 开始你的任务吧,祝你成功!
```cpp
#include <iostream>
using namespace std;
// 抽象类State
class State {
public:
virtual void idle() = 0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
// 角色类Hero
class Hero {
public:
Hero(State* state, int hp, int attack, int defense) {
this->state = state;
this->hp = hp;
this->attack = attack;
this->defense = defense;
}
void setState(State* state) {
this->state = state;
}
void idle() {
state->idle();
}
void run() {
state->run();
}
void attack() {
state->attack();
}
void dead() {
state->dead();
}
private:
State* state;
int hp;
int attack;
int defense;
};
// 怪物类Monster
class Monster {
public:
Monster(State* state, int hp, int attack, int defense, string type) {
this->state = state;
this->hp = hp;
this->attack = attack;
this->defense = defense;
this->type = type;
}
void setState(State* state) {
this->state = state;
}
void idle() {
cout << type << " is idling..." << endl;
}
void run() {
cout << type << " is running..." << endl;
}
void attack() {
cout << type << " is attacking..." << endl;
}
void dead() {
cout << type << " is dead..." << endl;
}
private:
State* state;
int hp;
int attack;
int defense;
string type;
};
// 站立状态类IdleState
class IdleState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 行走状态类RunState
class RunState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 攻击状态类AttackState
class AttackState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
// 死亡状态类DeadState
class DeadState : public State {
public:
void idle() {
cout << "hero is idling..." << endl;
}
void run() {
cout << "hero is running..." << endl;
}
void attack() {
cout << "hero is attacking..." << endl;
}
void dead() {
cout << "hero is dead..." << endl;
}
};
int main() {
State* idleState = new IdleState();
State* runState = new RunState();
State* attackState = new AttackState();
State* deadState = new DeadState();
Hero hero(idleState, 100, 10, 5);
Monster monster(idleState, 100, 10, 5, "Monster");
int input;
cin >> input;
if (input == 1) {
hero.idle();
hero.attack();
hero.run();
hero.dead();
} else if (input == 2) {
monster.idle();
monster.attack();
monster.run();
monster.dead();
}
return 0;
}
```
已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。 要求,使用抽象类及的基类指针实现。
好的,根据您的要求,我可以给您提供一个基于抽象类及基类指针的实现示例。请看下面的代码:
```c++
#include <iostream>
using namespace std;
// 游戏中常见人物状态State抽象类
class State {
public:
virtual void idle() = 0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
// 英雄类
class Hero : public State {
public:
void idle() {
cout << "Hero is idling..." << endl;
}
void run() {
cout << "Hero is running..." << endl;
}
void attack() {
cout << "Hero is attacking..." << endl;
}
void dead() {
cout << "Hero is dead..." << endl;
}
};
// 怪物类
class Monster : public State {
public:
void idle() {
cout << "Monster is idling..." << endl;
}
void run() {
cout << "Monster is running..." << endl;
}
void attack() {
cout << "Monster is attacking..." << endl;
}
void dead() {
cout << "Monster is dead..." << endl;
}
};
int main() {
int input;
State *state;
cout << "Please input 1 for Hero, 2 for Monster: ";
cin >> input;
if (input == 1) {
state = new Hero();
} else {
state = new Monster();
}
state->idle();
state->run();
state->attack();
state->dead();
delete state;
return 0;
}
```
在上面的代码中,我们定义了一个抽象类 State,其中包含了人物状态的常见动作,包括 Idle、Run、Attack 和 Dead 等。然后我们又定义了英雄类(Hero)和怪物类(Monster),它们都继承自 State 抽象类,并实现了其中的动作函数。
在 main 函数中,我们首先让用户输入 1 或 2,来选择是英雄还是怪物。然后根据用户的选择,我们使用 new 运算符动态地分配内存,并将基类指针 state 指向对应的对象。最后,我们通过调用 state 指针所指向对象的各种动作函数,来模拟角色在不同状态下的各种操作。最终,我们使用 delete 运算符释放内存,并结束程序。
希望这个示例能够帮助您理解抽象类及基类指针的使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)