这些超级陈述有什么问题?

时间:2015-02-15 15:21:57

标签: java class super

我一直在努力尝试并弄清楚这些代码行,但我似乎永远无法让它出来正确。我在第86,91,100,110和118行遇到了几个错误,我完全不知道为什么。无论如何,这是:

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;
}

public Car produceCar(String model)
{
    return name;
}

public Truck produceTruck(String model)
{
    return name;
}

}

class Vehicle
{
private 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
{
super (_make,_model);   //I get a illegal start of type and identifier           expected error here
}

class Truck extends Vehicle
{
super (_make,_model); //I get a illegal start of type and identifier expected error here
}

class Garage
{
//define a private variable that holds a Vehicle object. This will represent 
private String Veh;
// the vehicle being stored in the Garage. If the garage is empty, then this variable should be null
}
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
        {
            //store the vehicle that was passed to this function
            //in this class vehicle attribute
        }
}

class removeVehicle()
{
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 ??? in it!";
    }
    else
    {
        return "This garage is empty.";
    }
}

}

1 个答案:

答案 0 :(得分:1)

class Car extends Vehicle
{
  super (_make,_model);
}

上述super调用显示在构造函数外部。这在语法上是无效的代码。

解决方案1:删除此代码。

解决方案2:编写构造函数。