我有3个类,我想把它们放在一个数组中。该数组的长度为A,我想在此长度上定义前3个插槽仅从这些类中唯一。长度A由键盘给出。我只发布我遇到的代码。此代码位于我的主要和它的错误。立体声,Ygro,Aerio亚型食物。继承类到数组
Food[] pin = new Food[A]; // this the array that include all classes Stereo,Ygro,Aerio
for (int i = 0; i < 3; i++) {
pin[i] = new Stereo(); //this is a class start i=0
pin[i+1] = new Ygro(); // this is a class
pin[i+2] = new Aerio(); // this is a class
}
亚型*我想限定在此长度,所述前三个插槽只有这些类才是唯一的。*什么? – shmosel
'pin'的元素是'int'类型。你不能给它的元素赋一个'Stereo',只能写'int s = new Stereo();'。 –
Pin是一个int数组,因此您无法在其中放入对象。 –