簡體   English   中英

C ++中的多態和繼承

[英]Polymorphism and Inheritance in C++

所以在Java中,我有以下實現:

public abstract class PFigure implements Comparable

和我的繼承和多態的Java實現:

public class PFigureList
{
   private final int MAX_VEHICLES = 9;
   private PFigure list[] = new PFigure[MAX_VEHICLES];
   private int count = 0;

   /**
   Adds a PFigure to the list if the list is not full and increments the count.

   @param myFig The "vehicle" PFigure that is going to be added to the list
   */
   public void add(PFigure myFig)
   {
      if(count <= 9)
         list[count++] = myFig;
   }
/**
   For every figure in the list, it calls their hide() function, their 
   polymorphic move() function, and then their draw() function to show where 
   they are now.
   */
   public void move()
   {
      for(int i = 1; i < count; i++)
      {
         list[i].hide();
         list[i].move();
         list[i].draw();
      }
   }

現在,除了與C ++非常相似之外,我想完成的就是這一點。 這是我的代碼:

void VBotList::Add(VBot * add)
{
   vbot[count++] = add;
}

void VBotList::Move()
{
   /*for(int i = 0; i < list.GetCount(); i++)
      list.GetValue(i)->Move();*/

   for(int i = 0; i < count; i++)
      vbot[i]->Move();
}

class VBotList
{
   private:
     int count;

     VBot * vbot[50];

   public:
      VBotList() : count(0){ }
      void Add(VBot * vbot);
      void Move();
      void Show();

};

public ref class MyForm : public System::Windows::Forms::Form
{
public:
    MyForm(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }
  static VBotList * list = new VBotList();

我嘗試實現它:

   private: System::Void speedTimer_Tick(System::Object^  sender, System::EventArgs^  e) {
               list->Move();
               Invalidate();
               speedTimer->Interval = speedTrackBar->Value;
            }
private: System::Void vbotAddButton_Click(System::Object^  sender, System::EventArgs^  e) {
            if(comboBox1->SelectedIndex == 1)
            {
               VBot * x = new BillyBot(System::Convert::ToInt32(textBox1->Text), System::Convert::ToInt32(textBox2->Text), panel1);
               list->Add(x);
            }
         }
private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
            list->Show();
         }

我的目標是獲取一個存儲在VBotList中的VBot對象,就像我在Java中的PFigure對象的PFigureList一樣。 我不知道為什么,但是我無法理解它是否可以在面板上實際顯示我的對象。 我必須使VBotList的初始化為靜態,以便它不給我錯誤消息。 我是否遺漏了一些完全顯而易見的東西,或者只是在顯示對象時做了一些不正確的事情? 關於我的代碼的任何提示,建議或一記耳光都會很棒。

Show()基本上只顯示圖像。 如果需要,我將發布代碼。

您使用C ++ / CLI工作,因此您也必須管理類,例如:

ref class Bot
{
public:
    Bot()
    {

    }
};

ref class VBotList
{
private:
    int m_count;
    array<Bot ^> ^vbot;
public:
    VBotList() : m_count(0), vbot(gcnew array<Bot ^>(50))
    {
    }
    void Add(Bot ^newBot)
    {
        vbot[m_count++] = newBot;
    }
    int getCount()
    {
        return m_count;
    }
};

接下來:

private:
    VBotList ^list;

public:
    Form1(void)
    {
        InitializeComponent();
        list = gcnew VBotList();
    label1->Text = System::Convert::ToString(list->getCount());
    }

// ...
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    Bot ^bot = gcnew Bot();
    list->Add(bot);
    label1->Text = System::Convert::ToString(list->getCount());
}

^聲明托管指針的句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM