我正在编写一个程序,用于输出有关车辆信息的任务,其中Vehicle是Superclass,Car,Truck和Van是Subclasses。我们的讲师给了我们主要方法,所以我知道问题必须出在我为子类编写的代码中。
我已根据建议的反馈更新了我的代码,并在第3行遇到了新错误:无法访问AssignmentEX2类型的封闭实例。必须使用封闭的类型实例限定分配 AssignmentEX2(例如x.new A(),其中x是AssignmentEX2的实例)。
public class AssignmentEX2 {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle("5554EAWV3898");
System.out.println(vehicle.toString());
vehicle.rentVehicle();
System.out.println(vehicle.toString());
vehicle.returnVehicle();
System.out.println(vehicle.toString());
System.out.println();
Car car = new Car("6903NMME5853", 4);
System.out.println(car.toString());
car.rentVehicle();
System.out.println(car.toString());
System.out.println();
Truck truck = new Truck("7242OAHT0021", 26, 3);
System.out.println(truck.toString());
truck.rentVehicle();
System.out.println(truck.toString());
System.out.println();
Van van = new Van("5397NQRO4899", 11);
System.out.println(van.toString());
van.rentVehicle();
System.out.println(van.toString());
}
class Vehicle {
String vin;
Boolean rented = false;
public Vehicle(String vin) {
}
public String getVin() {
return vin;
}
public Boolean isRented() {
return rented;
}
public void rentVehicle() {
rented = true;
}
public void returnVehicle() {
rented = false;
}
public String toString() {
return("VIN: " + vin + "\nRented: " + rented );
}
}
class Car extends Vehicle {
int doors;
public Car (String vin, int doors) {
super( vin );
}
public int getDoors() {
return doors;
}
public String toString() {
return("VIN: " + vin + "\nRented: " + rented + "\nDoors: " + doors);
}
}
class Truck extends Vehicle {
int boxSize;
int axles;
public Truck(String vin, int boxSize, int axles) {
super( vin );
}
public int getBoxSize() {
return boxSize;
}
public int getAxles() {
return axles;
}
public String toString() {
return("VIN: " + vin + "\nRented: " + rented + "\nBox Size: " + boxSize + "\nAxles: "
+ axles);
}
}
class Van extends Vehicle {
int seats;
public Van (String vin, int seats) {
super( vin );
}
public int getSeats() {
return seats;
}
public String toString() {
return("VIN: " + vin + "\nRented: " + rented + "\nSeats: " + seats);
}
}
}