java - C++ 中的多态性和继承

标签 java object c++-cli

所以在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());
}

^ 声明托管指针的句柄。

关于java - C++ 中的多态性和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26436032/

相关文章:

c# - 选择/拾取在 Direct2D 中应该如何工作?

java - Eclipse 中的自定义输出文件夹,无需其他源文件夹

java - DateTimeFormatter.ISO_OFFSET_DATE_TIME 的等效格式字符串是什么?

object - customobject 和 psobject 之间的区别?

ruby - 如何理解#dup 和#clone 对引用其他对象的对象进行操作?

c# - 在 c++/cli dll 中引发事件并在 c# 中使用

java - 无法反序列化模型实例。PublisheDataList 超出 START_ARRAY token

java - 使用 Hibernate 二级缓存和 HSQL 的令人失望的插入/更新率

javascript - 如何重新格式化对象中的数据。 (我如何使行成为键,其他列成为值)

c++ - 删除数组时出现访问冲突异常