我们给了我们一个作业,它有2个(完整的)类MotorcycleCar,并要求在其中创建抽象类Vehicle,该类也应包含一个抽象方法(我猜想用于计算车税)和用于所有公共方法的名为CarIF的接口(我不确定这是什么意思)。

我创建了以下代码,并对MotorcycleCar进行了编辑,以使它们不包含颜色和cylinderCapacity,并创建了一个Test文件来查看其是否有效,但是当我尝试对其进行编译时,我得到了的错误

missing return statement


在我的Vehicle课上。

同样,每当我尝试在Motorcycle中将任何内容添加到Test.java时,都会出现错误,提示没有参数。那是因为我缺少构造函数,是否意味着我也需要在cylinderCapacity中向构造函数添加颜色和Car

(我将它们存放在单独的文件中,但是我将它们放在一起以将它们作为代码发布在这里。)

public abstract class Vehicle
{
    private String color;
    private double cylinderCapacity;

    public double getCylinderCapacity() {
            return cylinderCapacity;
        }

    public double setCylinderCapacity(double cylinderCapacity) { //Don't change the original issue otherwise anybody looking at this question may get confused.
            this.cylinderCapacity = cylinderCapacity;
    }


    public String getColor() {
            return color;
        }

    public void setColor(String color) {
            this.color = color;
        }


    public abstract double calculateMotorVehicleTax ();

    }

    public interface CarIF {

        void printInfo();

    }

    public class Motorcycle extends Vehicle implements CarIF {



    @Override
        public double calculateMotorVehicleTax() {
            return Math.ceil(cylinderCapacity / 25) * 1.84;
        }

        public void printInfo() {
            System.out.println("I'm a " + this.getClass().getSimpleName() + ".");
        }
    }


    public class Car extends Vehicle implements CarIF {

        private int engineType;
        private double co2Emission;

        public Car(int engineType,  double co2Emission) {

            this.engineType = engineType;
            this.co2Emission = co2Emission;
        }

        public int getEngineType() {
            return engineType;
        }

        public double getCo2Emission() {
            return co2Emission;
        }



        @Override
        public double calculateMotorVehicleTax() {
            double tax = (engineType == 0) ? 2.0 : 9.5;
            double _co2Emission = co2Emission - 95;
            if(_co2Emission < 0) {
                _co2Emission = 0;
            }
            return Math.ceil(cylinderCapacity / 100) * tax + _co2Emission * 2.0;
        }

        public void printInfo() {
            System.out.println("I'm a " + this.getClass().getSimpleName() + ".");
        }
    }

    public class Test {
    public static void printTax(Vehicle o) {
    System.out.print("Tax: ");
    System.out.println(o.calculateMotorVehicleTax());
    }

    public static void main(String[] args) {
    Car c = new Car("blue", red, 3, 5);
    Motorcycle m = new Motorcycle("red", 9,);
    printTax(c);
    printTax(m);
    }
}

最佳答案

您应该在Vehicle类,MotorCycle类中添加一个构造函数,并在car类中更改该构造函数。您的Vehicle构造函数应如下所示

public Vehicle(String color, Double cylinderCapacity){
this.color = color;
this.cylinderCapacity = cylinderCapacity;
}


您的MotorCycle构造函数应如下所示:

public MotorCycle(String color, double cyclinderCapacity){
super(color, cylinderCapacity)//this calls the vehicle constructor
}


您的汽车构造函数与测试中的参数不匹配。如果要在汽车构造函数中添加color和cylindeCapacity,只需在参数中添加color和CylinderCapacity,然后在构造函数主体中调用超级构造函数,就像在MotorCycle构造函数中一样。

另外,您的double setCylinderCapacity(double cylinderCapacity)方法缺少返回类型。我假设方法只是设置值,所以只需将double更改为void例如

void setCylinderCapacity(double cylinderCapacity)


希望这可以帮助

10-06 14:58