2014-10-18 94 views
0

所以在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对象,它就像我的PFigure对象PFigureList在Java中。我不知道为什么,但我无法让它实际在面板上显示我的对象。我不得不使VBotList静态初始化,以便它不给我错误消息。我是否错过了一些完全明显的东西,或者我只是在显示对象时做了些不正确的事情?任何关于我的代码的提示,建议或一巴掌都会很棒。

显示()基本上只显示图像。如果需要,我会发布代码。

回答

1

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

^声明的句柄托管指针。