谁能指出为什么我的领域会抛出错误?

Can anybody point out why my field is throwing errors?

我目前正在做一个小项目来提高我的 Java 技能,并选择了一个小项目来测试你设计和实施 Java classes 建模的能力和操作数据。 我必须创建 classes 来管理汽车租赁公司的一系列汽车租赁预订。我必须使用继承层次结构和来自 Java 集合框架 (JCF) 的适当集合类型来开发 classes。

到目前为止,我已经创建了我的第一个 class,它是 Vehicle class,它模拟了出租车辆,第二个 class 模拟了车辆的租用。

我在 class 之间互相交谈时遇到了一些麻烦,希望有人能为我指出正确的方向。我的车辆 class 没问题,没有错误,问题出在租车 class 上,我似乎无法让我的 'Vehicle' 字段正常运行。下面是我的两个 classes.

的代码

非常感谢您的帮助

//Import the comparator 
import java.util.Comparator;


//Task 1 - Vehicle Class

   


        public class Vehicle {

    //Private string field that stores the name of the vehicle
    //Private double field that stores the cost per day for vehicle hire
    private String modelName;
    private double dailyCost;

    //Constructor for vehicle with two fields using two parameters
    public Vehicle(String Modelname, double dailyCost) {
        this.modelName = modelName;
        this.dailyCost = dailyCost;
    }

    //Getter for model name
    public String getModelname() {
        return modelName;
    }

    //Setter for model name
    public void setModelName() {
        this.modelName = modelName;
    }

    //Getter for daily cost
    public double getDailyCost() {
        return dailyCost;
    }

    //Setter for daily cost
    public void setDailyCost() {
        this.dailyCost = dailyCost;
    }

    @Override
    //Return details as formatted String 
    public String toString(){
       String result = "Vehicle: " + this.getModelname()+ " £" + this.getDailyCost() + " per day\n";
       return result;
    }
    
    
    
}




//Task 2 - Abstract Hire class

        public abstract class Hire {

    //Private field of type vehicle to store the vehicle on hire
    //Private int to store the duration of hire in days 
    private int days;
    //Need to declare this field properly later 
    private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost);
    
    
    //Constructor for the Hire with two fields and two parameters
    public Hire(String vehicle, int days) {
        this.vehicle = vehicle;
        this.days = days;
    }

    //Getter for model days
    public int getDays() {
        return days;
    }

    //Setter for model days
    public void setdays() {
        this.days = days;
    }
    
    //Getter for model vehicle
    public String getVehicle() {
        return vehicle;
    }

    //Setter for model vehicle
    public void setVehicle() {
        this.vehicle = vehicle;
    }
    
    //Method to calculate the total cost of hire 
    public double getCost(){
        return (days * dailyCost);
    }
    
    
    //Think this is in the right place?
    @Override
    //Method to return the details of Hire as a string 
    public String toString(){
        String result = ("Vehicle: " + this.modelName()+ this.dailyCost + " per day. Days: " + this.getDays + " Hire Cost: " + this.getCost );
       return result;
    }
    
}

您需要将实际值传递给 Hire 内部的 Vehicle 构造函数 例如:

new Vehicle("Toyota", 100);

我使用 Lambda 创建一个 instace Object hire 来扩展抽象 class Hire 并且 over-write setter/getter method.Now 它有效。

雇用 Class:

public abstract class Hire {
    //Private field of type vehicle to store the vehicle on hire
    //Private int to store the duration of hire in days
    private int days;
    private String vehicle;

    private int dailyCost;
    //Need to declare this field properly later
    private Vehicle aVehicle;

    // a blank Constructor
    public Hire(){

    }

    //Constructor for the Hire with two fields and two parameters
    public Hire(String vehicle, int days) {
        this.vehicle = aVehicle.getModelname();
        this.days = days;
    }

    //Getter for model days
    public int getDays() {
        return days;
    }

    //Setter for model days
    public void setdays() {
        this.days = days;
    }

    //Getter for model vehicle
    public String getVehicle() {
        return vehicle;
    }

    //Setter for model vehicle
    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle.getModelname();
    }

    //Method to calculate the total cost of hire
    public double getCost(){
        return (days * dailyCost);
    }


    //Think this is in the right place?
    @Override
    //Method to return the details of Hire as a string
    public String toString(){
        String result = ("Vehicle: " + this.aVehicle.getModelname()+ this.dailyCost + " per day. Days: " + this.getDays() + " Hire Cost: " + this.getCost() );
        return result;
    }
}

测试Class:

public class Test {
    public static void main(String[] args){
        Vehicle vehicle = new Vehicle("Driver",100);
        Hire hire = new Hire() {
            @Override
            public int getDays() {
                return super.getDays();
            }

            @Override
            public void setVehicle(Vehicle vehicle) {
                super.setVehicle(vehicle);
            }

            @Override
            public String getVehicle() {
                return super.getVehicle();
            }
        };

        hire.setVehicle(vehicle);
        System.out.println("Vehivle Class Name: " + vehicle.getModelname());

        System.out.println("Abstract Hire Class Name: " + hire.getVehicle());
    }
}

输出为:

Vehivle Class Name: Driver
Abstract Hire Class Name: Driver

正如@tobias-k 所说:new Vehicle(String modelName, double dailyCost); 无效。您可以通过 Hire constructor

传递 Vehicle 实例
private Vehicle vehicle;

public Hire(Vehicle vehicle, int days) {
    this.vehicle = vehicle;
    this.days = days;
}

此外,我修复了这些方法:

public Vehicle getVehicle() { //Valid return type
    return vehicle;
}

public void setVehicle(Vehicle vehicle) { //Pass vehicle instance to set as this.vehicle value
    this.vehicle = vehicle;
}

public void setdays(int days) { //Pass int valueto set as this.days value
    this.days = days;
}

public double getCost(){
    return (days * this.vehicle.getDailyCost()); //Valid get dailyCost via Vehicle getter
}

@Override
public String toString() { //Accesses via getters and formatted
    return "Vehicle: " + this.vehicle.getModelname() + this.vehicle.getDailyCost() +
            " per day. Days: " + this.days + " Hire Cost: " + this.getCost();
}

车辆修复class:

  1. (modelName 打字错误)

而不是:

public Vehicle(String Modelname, double dailyCost) {
        this.modelName = modelName;
        this.dailyCost = dailyCost;
    }

使用:

public Vehicle(String modelName, double dailyCost) {
    this.modelName = modelName;
    this.dailyCost = dailyCost;
}

2.(缺少`String modelName`)

而不是:

public void setModelName() {
    this.modelName = modelName;
}

使用:

public void setModelName(String modelName) {
    this.modelName = modelName;
}

3.(缺少`double dailyCost`)

而不是:

public void setDailyCost() {
    this.dailyCost = dailyCost;
}

使用:

public void setDailyCost(double dailyCost) {
    this.dailyCost = dailyCost;
}

尝试更改:

  • private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost);private Vehicle aVehicle = new Vehicle("", (Double) 0);null 因为你需要实例化一些东西,而不是传递构造函数;

  • public Hire(String vehicle, int days) {public Hire(Vehicle vehicle, int days) { 因为你需要 Vehicle class 而不是 String;

  • this.vehicle = vehicle;this.aVehicle = vehicle; 因为 aVehicleHire class.

    中的变量名

首先你在 'Vehicule' class 的构造函数是错误的

public Vehicle(String Modelname, double dailyCost) {
        this.modelName = modelName;
        this.dailyCost = dailyCost;
    }

参数'String Modelname'写的是大写的M,但是你给的'modelname''this.modelname'其实就是class属性, 就像你在做的那样:this.modelName=this.modelName 这意味着您没有向其中添加任何新内容

其次,如果您愿意使用它,为什么您的 'Hire' class 是抽象的?抽象 class 无法初始化。

我在这个class中还发现了两个主要错误:

  1. private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost);
    在这里你必须首先声明 String 'modelname' 和 double 'dailyCost' 并在创建你的 aVehicle object.
  2. 之前给它们一个值

-或者只是这样做:Vehicle aVehicle = new Vehicle("Volkswagen",42069);

  1. 你的 Vehicule 的 getter 和 setter 是错误的,因为你将你的 Vehicle 属性 声明为 aVehicle 并且带有 'a' 而不是 '车辆'

您的 getter 和 setter 应该像这样:

 public String getaVehicle() {
      return aVehicle;
  }

  //Setter for model vehicle
  public void setaVehicle(Vehicle aVehicle) {
      this.aVehicle = aVehicle;
  }

最后,您在 class 中的 toString 方法是错误的,因为同样的初始错误,您没有声明 modelNamedailycost Hire class 中的属性。 现在如果你想在两个 classes 之间继承,你应该这样做:

public class Hire extends Vehicle {
...
}

这个继承意味着你的childclass'Hire'class将 'inherite' 您的 parent class Vehicle class 中的所有属性和方法,因此您不必再次在 Hire class 中声明 modelname 和 dailycost 属性,因为它们已经存在于您的 Vehicle class.