java - 返回中的不兼容类型错误给我错误?

标签 java types incompatibility

所以我已经尝试处理这段代码一段时间了,但是我的两个返回函数无法正常工作。我知道我没有返回正确的类型,但我完全不知道要返回什么来代替它,这里是代码:错误位于第 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/

相关文章:

java - 整数的最大值

Python:转换字符串以用于 ctypes.c_void_p()

android - 仅在付费版本上“此应用程序与您的所有设备不兼容”?

java - Netbeans 不兼容类型问题

java - 如何修复 java.lang.UnsupportedClassVersionError : Unsupported major. 次要版本

java - 代码没有错误,但模拟器表示应用已停止

java - 使用 OpenGL 进行纹理颜色操作(在使用 Java 的 LWJGL 中)

java - 在 Java 中创建类型的通用实例

c# - 类型操作

java - 分离 Yourkit session