一、什么是多态:同一个实体,同时具有多种形式
- 向不同的对象发送同一条消息(即函数调用)
- 不同的对象在接收时会产生不同的行为(即不同的函数实现)
二、多态的两种方式
1. 函数重载的方式(静态多态或编译时多态)
//使用函数重载的方式
//静态多态或是编译时多态
void MoveRole(const Warrior &warrior)
{
warrior.Move();
}
void MoveRole(const Archmage &archmage)
{
archmage.Move();
}
template <typename T> void MoveRole(const T& vec)
{
for (auto it : vec)
{
it->Move();
}
}
2. 函数重写的方式(动态多态或运行时多态)
-
要实现C++函数重写,必须要先把父类的成员函数设定为虚函数
virtual 返回值 函数名();
注意1:
- 派生类重写基类方法Move()时可以加上override关键字表示重写
- override关键字为C++11标准后新加入的,用来明确派生类重写基类继承过来的虚函数
注意2:
- 为了能够让同一个函数操作不同类型的子类对象,所以我们把参数类型定义成了基类对象
- 当传递Hero类型的子类类型时,参数类型可以自动转化(子类对象可以赋值给父类对象)
void MoveRole(const Hero &hero) { //传参时可以是任意的派生类对象 hero.Move(); }
向上转型:把子类对象转换成父类对象
- 向上转型是安全的
- 向上转型可以自动完成(自动类型转换)
- 向上转型的过程中会丢失子类信息
向下转型:把子类对象强制转换成父类对象 不安全
三、代码示例
#pragma once
#include <iostream>
#include <string>
using namespace std;
/*位置类(坐标类)*/
class Point
{
public:
Point();
Point(int x, int y);
~Point();
int GetX() { return m_x; }
int GetY() { return m_y; }
void SetX(int x) { m_x = x; }
void SetY(int y) { m_y = y; }
friend ostream & operator<< (ostream &out, const Point &position)
{
out << "(" << position.m_x << "," << position.m_y << ")";
return out;
}
private:
int m_x;
int m_y;
};
Point::Point()
{
}
Point::Point(int x, int y):m_x(x),m_y(y)
{
}
Point::~Point()
{
}
/*战斗单位类*/
class BattleUnit
{
public:
BattleUnit();
BattleUnit(const string &name);
~BattleUnit();
const string &GetName() { return m_strName; }
void SetPoint(int x, int y) { m_point = Point(x, y); }
const Point &GetPoint() { return m_point; }
//纯虚函数 不能直接实例化对象
virtual void Fight(BattleUnit &other) = 0;
virtual void Move(int x, int y) = 0;
virtual void Move(const Point &position) = 0;
private:
string m_strName;
int m_maxHp;
int m_curHp;
int m_attDistance; //当前对象的攻击距离
Point m_point; //当前对象坐标值
};
BattleUnit::BattleUnit(const string& name):m_strName(name),m_point(0,0)
{
m_maxHp = 100;
m_curHp = 100;
m_attDistance = 100;
}
BattleUnit::BattleUnit()
{
}
/*陆战队员类*/
BattleUnit::~BattleUnit()
{
}
class Marine : public BattleUnit
{
public:
Marine();
Marine(const string &name);
~Marine();
void Fight(BattleUnit &other) override;
void Move(int x, int y) override {};
void Move(const Point &position) override {};
private:
};
Marine::Marine()
{
}
Marine::Marine(const string &name) : BattleUnit(name)
{
}
Marine::~Marine()
{
}
void Marine::Fight(BattleUnit &other)
{
//在子类中调用父类的同名方法
//BattleUnit::Fight(other);
cout << "陆战队员:" << GetName() << "正在攻击敌人" << other.GetName() << endl;
}
/*攻城坦克*/
class SiegeTank : public BattleUnit
{
public:
SiegeTank();
SiegeTank(const string &name);
~SiegeTank();
virtual void Fight(BattleUnit &other) {};
virtual void Move(int x, int y);
virtual void Move(const Point &position) {};
private:
};
SiegeTank::SiegeTank()
{
}
SiegeTank::SiegeTank(const string &name) : BattleUnit(name)
{
}
SiegeTank::~SiegeTank()
{
}
void SiegeTank::Move(int x, int y)
{
SetPoint(x, y);
cout << "攻城坦克:" << GetName() << "收到移动命令" << GetPoint() << endl;
}