有人可以指出为什么我的字段会报错吗?

huangapple go评论78阅读模式
英文:

Can anybody point out why my field is throwing errors?

问题

I'm currently working on a small project to improve my Java skills and settled on a small project which tests your ability to design and implement Java classes to model and manipulate data.
我目前正在进行一个小项目,以提高我的Java技能,并选择了一个小项目,测试您设计和实现Java类来模拟和操作数据的能力。

I have to create classes to manage a collection of vehicle hire bookings for a vehicle-hire company. I have to develop classes using an inheritance hierarchy, and appropriate collection types from the Java Collections Framework (JCF).
我必须创建用于管理车辆租赁公司的车辆租赁预订的类。我必须使用继承层次结构和来自Java集合框架(JCF)的适当集合类型来开发类。

So far I've created my first class which is the Vehicle class which models a vehicle for hire, and a second class which models the hiring of a vehicle.
到目前为止,我已经创建了我的第一个类,即Vehicle类,该类用于模拟可租用的车辆,以及一个用于模拟车辆租赁的第二个类。

I'm having a bit of trouble making my classes talk to each other and was hoping somebody could point me in the right direction. My vehicle class is fine and has no errors, the problem is on the hire class where I don't seem to be able to get my 'Vehicle' field to function properly. Below is the code for my two classes.
我在使我的类彼此通信方面遇到了一些问题,希望有人可以指导我正确的方向。我的车辆类没有问题,没有错误,问题出在租赁类上,我似乎无法使我的'Vehicle'字段正常工作。以下是我两个类的代码。

Thanks a lot in advance for any help
非常感谢您提前提供的任何帮助

//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(String modelName) {
        this.modelName = modelName;
    }

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

    //Setter for daily cost
    public void setDailyCost(double dailyCost) {
        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 vehicle;

    //Constructor for the Hire with two fields and two parameters
    public Hire(Vehicle 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(int days) {
        this.days = days;
    }

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

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

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

    //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.vehicle.getModelName() + " £" + this.vehicle.getDailyCost() + " per day. Days: " + this.getDays() + " Hire Cost: " + this.getCost();
        return result;
    }
}
英文:

I'm currently working on a small project to improve my Java skills and settled on a small project which tests your ability to design and implement Java classes to model and manipulate data.
I have to create classes to manage a collection of vehicle hire bookings for a vehicle-hire company. I have to develop classes using an inheritance hierarchy, and appropriate collection types from the Java Collections Framework (JCF).

So far I've created my first class which is the Vehicle class which models a vehicle for hire, and a second class which models the hiring of a vehicle.

I'm having a bit of trouble making my classes talk to each other and was hoping somebody could point me in the right direction. My vehicle class is fine and has no errors, the problem is on the hire class where I don't seem to be able to get my 'Vehicle' field to function properly. Below is the code for my two classes.

Thanks a lot in advance for any help

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

答案1

得分: 0

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

new Vehicle("Toyota", 100);
英文:

You need to pass to the Vehicle constructor inside Hire actual values
for example:

new Vehicle("Toyota", 100);

答案2

得分: 0

I use Lambda to create an instance of Hire class to extend the abstract class Hire and override setter/getter methods. Now it works.

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

Test 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("Vehicle Class Name: " + vehicle.getModelname());

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

The output is :

Vehicle Class Name: Driver
Abstract Hire Class Name: Driver
英文:

I use Lambda to create a instace Object hire to extend abstract class Hire and over-write setter/getter method.Now it works.

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

Test 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());
    }
}

The output is :

Vehivle Class Name: Driver
Abstract Hire Class Name: Driver

答案3

得分: 0

如 @tobias-k 所说: new Vehicle(String modelName, double dailyCost); 不是有效的。您可以通过 Hire 构造函数传递 Vehicle 实例<br>

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

另外,我修复了以下这些方法:

public Vehicle getVehicle() { // 有效的返回类型
return vehicle;
}
public void setVehicle(Vehicle vehicle) { // 传递 vehicle 实例以设置 this.vehicle 值
this.vehicle = vehicle;
}
public void setdays(int days) { // 传递 int 值以设置 this.days 值
this.days = days;
}
public double getCost(){
return (days * this.vehicle.getDailyCost()); // 通过 Vehicle 的 getter 有效获取 dailyCost
}
@Override
public String toString() { // 通过 getters 访问并格式化
return "Vehicle: " + this.vehicle.getModelname() + this.vehicle.getDailyCost() +
" per day. Days: " + this.days + " Hire Cost: " + this.getCost();
}

Vehicle 类的修复:

<br>

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

<br>
2. (缺少 String modelName)

而不是:

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

使用 :

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

<br>
3. (缺少 double dailyCost)

而不是:

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

使用:

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

As @tobias-k said: new Vehicle(String modelName, double dailyCost); is not valid. You can pass Vehicle instance by Hire constructor<br>

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

Additionaly I fixed those methods:

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 &quot;Vehicle: &quot; + this.vehicle.getModelname() + this.vehicle.getDailyCost() +
&quot; per day. Days: &quot; + this.days + &quot; Hire Cost: &quot; + this.getCost();
}

Fixes for Vehicle class:
<br>

  1. (modelName typo)

instead of :

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

use:

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

<br>
2. (lack of String modelName)

instead of :

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

use :

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

<br>
3. (lack of double dailyCost)

instead of :

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

use:

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

答案4

得分: 0

  • 尝试将 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 类而不是一个 String

  • this.vehicle = vehicle; 更改为 this.aVehicle = vehicle;,因为在 Hire 类中,aVehicle 是变量的名称。

英文:

Try change:

  • private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost); with private Vehicle aVehicle = new Vehicle(&quot;&quot;, (Double) 0); or null because you need to instanciate something, and not pass the contructor;

  • public Hire(String vehicle, int days) { with public Hire(Vehicle vehicle, int days) { because you need a Vehicle class and not a String;

  • this.vehicle = vehicle; to this.aVehicle = vehicle; because aVehicle is the name of variable in Hire class.

答案5

得分: 0

首先,你在'Vehicle'类中的构造函数是错误的:

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

参数 'String Modelname' 使用了大写的 'M',但是你给 'this.modelname' 的赋值实际上是类属性,就好像你在做:this.modelName=this.modelName,这意味着你没有添加任何新内容。

其次,如果你想要使用 'Hire' 类,为什么要将它设置为抽象类呢?抽象类无法被初始化。

我还在这个类中找到了两个主要错误:

  1. private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost);
    在这里,你必须先声明 String 'modelname' 和 double 'dailyCost',并给它们赋值,然后再创建 aVehicle 对象。
  • 或者可以这样做:Vehicle aVehicle = new Vehicle("Volkswagen", 42069);
  1. 你的 Vehicule 类的 getter 和 setter 是错误的,因为你将 Vehicle 属性声明为 aVehicle 而不是 vehicule

你的 getter 和 setter 应该像这样:

public Vehicle getaVehicle() {
    return aVehicle;
}

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

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

public class Hire extends Vehicle {
...
}

这个继承意味着你的 子类 'Hire' 将从 父类 Vehicle 中继承所有的属性和方法,所以你不需要在 Hire 类中再次声明 modelname 和 dailycost 属性,因为它们已经在 Vehicle 类中存在。

英文:

First of all your constructor at 'Vehicule' class is wrong

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

The parameter 'String Modelname' is written with a capital M, but the 'modelname' you're giving to 'this.modelname' is actually the class property,
it's like you're doing : this.modelName=this.modelName
Which means you're adding nothing new to it

Secondly, why would your 'Hire' class be abstract if you're willing to use it ? an abstract class can't be initialized.

I also found two main errors in this class :

  1. private Vehicle aVehicle = new Vehicle(String modelName, double dailyCost);
    Here you must first declare the String 'modelname' and double 'dailyCost' and give them a value before creating your aVehicle object.

-Or just do this instead : Vehicle aVehicle = new Vehicle(&quot;Volkswagen&quot;,42069);

  1. your getters and setters for Vehicule are wrong because you declared your Vehicle property as aVehicle with an 'a' and not 'vehicule'

Your getters and setters should be like this instead :

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

Finally, your toString method in this class is wrong because of the same initiale error, you didn't declare the modelName and dailycost properties in your Hire class.
Now if you want a inheritance between the two classes, you should do this :

public class Hire extends Vehicle {
...
}

This inheritance means that your child class 'Hire' class will 'inherite' all the attributes and methods from your parent class Vehicle class, so you won't have to declare the modelname and dailycost attributes in Hire class again, because they are already present in your Vehicle class.

huangapple
  • 本文由 发表于 2020年8月5日 17:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定