我正在尝试从 Car 的父类中获取一个参数并将其添加到我的数组 ( carsParked
) 中,我该怎么做?
家长班
public class Car
{
protected String regNo; //Car registration number
protected String owner; //Name of the owner
protected String carColor;
/** Creates a Car object
* @param rNo - registration number
* @param own - name of the owner
**/
public Car (String rNo, String own, String carColour)
{
regNo = rNo;
owner = own;
carColor = carColour;
}
/** @return The car registration number
**/
public String getRegNo()
{
return regNo;
}
/** @return A String representation of the car details
**/
public String getAsString()
{
return "Car: " + regNo + "\nColor: " + carColor;
}
public String getColor()
{
return carColor;
}
}
儿童班
public class Carpark extends Car
{
private String location; // Location of the Car Park
private int capacity; // Capacity of the Car Park - how many cars it can hold
private int carsIn; // Number of cars currently in the Car Park
private String[] carsParked;
/** Constructor for Carparks
* @param loc - the Location of the Carpark
* @param cap - the Capacity of the Carpark
*/
public Carpark (String locations, int room)
{
location = locations;
capacity = room;
}
/** Records entry of a car into the car park */
public void driveIn()
{
carsIn = carsIn + 1;
}
/** Records the departure of a car from the car park */
public void driveOut()
{
carsIn = carsIn - 1;
}
/** Returns a String representation of information about the carpark */
public String getAsString()
{
return location + "\nCapacity: " + capacity +
" Currently parked: " + carsIn +
"\n*************************\n";
}
}
最后一题方法
公共字符串 getCarsByColor(字符串 carColour)
{
for (int num = 0; num < carsParked.length; num++) { if ( carColour.equals(carsParked[num]) ) { System.out.print (carsParked[num]);
}
}
return carColour;
}
到目前为止我有这个,所以如果在参数中输入“红色”,它会列出所有红色的汽车及其相应的信息,但似乎不起作用〜_〜。