Java C+的多态性与遗传+;
因此,在java中,我有以下实现:Java C+的多态性与遗传+;,java,object,c++-cli,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; /** Ad
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();
}
}
我在这里的目标是获取一个VBot对象,它存储在一个VBotList中,就像我在java中的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: 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();
}
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());
}