在同一个班上的多态性吗? [英] In class polymorphism of another class?

查看:45
本文介绍了在同一个班上的多态性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是想让一个类拥有另一个类作为数据成员,但我不知道该数据成员将是什么样的类.例如:

为了可读性,我遗漏了很多明显的代码.

so im trying to make a class have another class as a data member only I do not know the type of class that this data member will be. example:

I left out a lot of obvious code for readability.

class weapon
{
    public:
      string gettype()
      {
       return type;
       }
    private:
      string type;

};

class sword : weapon
{
   public:

   private:
     string type = "sword";

};

class knife : weapon
{
   public:
   private:
     string type = "knife";
};

class unit
{
  public:
    void setequippedweapon( how_do_i_do_this  )  // attempting to assign a unknown class as a data member?
    {
     equippedweapon = how_do_i_do_this;
     }
  private:
    how_do_i_do_this equippedweapon;  // attempting to declare the unknown class data member?
}

int main()
{
  unit soldier;
  weapon myknife;
  weapon mysword; // i will send one of these to be equipped
  soldier.setequippedweapon(how_do_i_do_this);   // since this could be a knife, sword, or whatever other weapon class i make in the future, how do i declare and use this?
  cout << "soldiers weapon is a: " << soldier.equippedweapon.gettype(); // and how to access the weapon''s member functions/variables through the soldier?
  return 0;
}

推荐答案

void setequippedweapon( how_do_i_do_this  )



可以简单地替换为



can simply be replace by

void setequippedweapon( weapon  w )
{
    equippedweapon = w;
}
private:
weapon equippedweapon;



然后像
一样使用它



then use it like

soldier.setequippedweapon(knife);



之所以可行,是因为所有武器类别都源自相同的基础类别武器.您可以使用多态性来使用特定武器的实际属性.



This works because all weapon classes derive from the same base class, weapon. The you make use of polymorphism to use the actual attributes of that particular weapon.


您可以在单元类中使用基类指针进行操作:):

You can operate with the base class pointers in your unit class :) :

class CWeapon
{
protected:
  CString m_cszType;

public:
  CWeapon() { m_cszType = _T("yet empty"); }

  const CString& GetType() { return m_cszType; }

  // optional, important for the instances assignments
  const CWeapon& operator = (const CWeapon& other) {
    m_cszType = other.m_cszType;
    return *this;
  }
};

class CSword : public CWeapon
{
public:
  CSword() { m_cszType = _T("sword"); }
};

class CSword : public CWeapon
{
public:
  CKnife() { m_cszType = _T("knife"); }
};

class CUnit
{
  CWeapon* m_pcWeapon;
  bool m_bWeaponOwner;

public:
  CUnit() {
    m_pcWeapon = NULL;
    m_bWeaponOwner = false;
  }
  
  ~CUnit() {
    if (m_bWeaponOwner && m_pcWeapon) {
      delete m_pcWeapon;
    }
  }

  CWeapon* GetWeapon() { return m_pcWeapon; }

  bool SetWeapon(CWeapon* pcWeapon, bool bOwner = true) {
    bool bResult = (NULL != pcWeapon);
    if (bResult) {
      if (m_bOwner && m_pcWeapon) {
        delete m_pcWeapon; // old
        m_pcWeapon = NULL;
      }
      m_bOwner = bOwner;
      m_pcWeapon = pcWeapon; // new
    }
    return bResult;
  }
};

int main()
{
  CUnit aSoldier;
  CKnife aKnife;
  
  aSoldier.SetWeapon(&aKnife, false); // the knife does not die with the soldier
  ASSERT(aSoldier.GetWeapon()->GetType() == _T("knife"));

  aSoldier.SetWeapon(new CSword(), true); // the sword dies with the soldier
  ASSERT(aSoldier.GetWeapon()->GetType() == _T("sword"));
  
  return 0;
}



...但是我认为这还不是理论上的多态性:)



...but it is not the theoretical polymorphism yet, I think :)


怎么样(我只是稍微修改一下代码,但我不是)说这是最好的方法,就我个人而言,我会组织不同的事情):
How about something like (I''ll just modify your code a little bit, but I''m not saying that this is the best way to go, personally I would organize things different):
class weapon
{
public:
    weapon() : type("unknown") {}
    explicit weapon(const string& t) : type(t) {}
    virtual ~weapon() {}

    string gettype() const
    {
       return type;
    }
      
private:
    string type;
};

class sword : public weapon
{
public:
    sword() : weapon("sword");

    // Here you can add specific things to sword...
};

class knife : public weapon
{
public:
    knife() : weapon("knife");

   // Here you can add specific things to knife...
};

class unit
{
public:
    void setequippedweapon(const weapon* w)
    {
        equippedweapon = w;
    }
    
    weapon* getequippedweapon() const
    {
        return equippedweapon;
    }

private:
    weapon* equippedweapon;
}

int main()
{
    unit soldier;
    knife myknife;
    sword mysword; // i will send one of these to be equipped
  
    soldier.setequippedweapon(&mysword);
    cout << "soldiers weapon is a: " << soldier.getequippedweapon()->gettype();
    return 0;
}



我希望这能使您对如何完成这些事情有所了解.但是IMO您应该拿起一本好的编程书并学习基本概念.您也可以使用来自 Internet 的资源,但是一本好书会更有用.

祝您编码愉快! :)



I hope that this will give you an idea on how the things can be done. But IMO you should pick up a good programming book and learn basic concepts. You could also use resources from Internet but a good book would be more useful.

Happy coding! :)


这篇关于在同一个班上的多态性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆