所以我已经尝试处理这段代码一段时间了,但是我的两个返回函数无法正常工作。我知道我没有返回正确的类型,但我完全不知道要返回什么来代替它,这里是代码:错误位于第 53 行和第 58 行。我需要什么代码才能正确返回汽车和卡车?
public class lab21composition
{
public static void main(String args[])
{
CarFactory factory = new CarFactory ("Ford");
Garage myGarage = new Garage();
Car c = factory.produceCar("Fusion");
Truck t = factory.produceTruck("F150");
System.out.println(myGarage);
myGarage.addVehicle(c);
System.out.println(myGarage);
myGarage.addVehicle(t);
System.out.println(myGarage);
Vehicle v = myGarage.removeVehicle();
if (null !=v)
{
System.out.println(v.toString() + " was removed from garage.");
System.out.println(myGarage);
}
else
{
System.out.println("There was no vehicle in the garage to remove.");
}
myGarage.addVehicle(t);
System.out.println(myGarage);
CarFactory factory2 = new CarFactory ("Honda");
Garage myGarage2 = new Garage();
Car d = factory.produceCar("Odyssy");
myGarage.addVehicle(d);
System.out.println(myGarage2);
}
}
class CarFactory
{
private String name;
public CarFactory(String n)
{
name = n; //ERROR, incompatible types/ Required:Truck/ Found: java.lang.String
}
public Car produceCar(String model)
{
return name; //ERROR, incompatible Types/ Required:Truck/Found: java.lang.String
}
public Truck produceTruck(String model)
{
return name;
}
}
class Vehicle
{
public static String make, model;
public Vehicle()
{
make = "Undefined";
model = "Undefined";
}
public Vehicle(String _make, String _model)
{
make = _make;
model = _model;
}
public String toString()
{
return make+" "+model;
}
}
class Car extends Vehicle
{
public Car()
{
super (make,model);
}
}
class Truck extends Vehicle
{
public Truck()
{
super (make,model);
}
}
class Garage
{
// Define a private variable that holds a Vehicle object. This will represent the vehicle being stored in the Garage. If the garage is empty, then this variable
// should be null
private Vehicle Veh;
public void addVehicle(Vehicle v)
{
if(v==Veh) /* replace FALSE with code to check if v is the same as vehicle */
{
//HINT: use a function inherited from the Object class!
System.out.println(v.toString() + " is already parked in this garage");
}
else if (hasVehicle())
{
System.out.println("This garage is full!");
}
else
{
Veh = v;
//store the vehicle that was passed to this function
//in this class vehicle attribute
}
}
public Vehicle removeVehicle()
{
//store this class vehicle attribute in a temporary variable
// set this class vehicle attribute to null
// return the vehicle stored in the temporary variable
return null;
}
public boolean hasVehicle()
{
//change this return statement so that it
//returns an appropriate boolean value
return false;
}
public String toString()
{
if (hasVehicle())
{
//replace ??? with the toString() method of the vehicle
// that is in this garage
return "This garage has a"+toString()+"in it!";
}
else
{
return "This garage is empty.";
}
}
}
最佳答案
您的问题在这里:
class CarFactory
{
private String name;
//snip
public Car produceCar(String model)
{
return name;
}
public Truck produceTruck(String model)
{
return name;
}
}
您声明 CarFactory.productCar
返回 Car
- 但您要 return name;
并且 name 类型为 >字符串
。
关于java - 返回中的不兼容类型错误给我错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28545228/