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靜態初始化,以便它不給我錯誤消息。我是否錯過了一些完全明顯的東西,或者我只是在顯示對象時做了些不正確的事情?任何關於我的代碼的提示,建議或一巴掌都會很棒。
顯示()基本上只顯示圖像。如果需要,我會發布代碼。