我正在做下面的家庭作业(我对 Java 还很陌生......),我在找出编写 LuxuryCarRental 的正确方法时遇到了一些问题......我不确定是否可以我在 CarRental 的工作是正确的。我还没去过 UseCarRental。
创建一个名为 CarRental 的类,其中包含以下字段:renterName、zipCode、carSize、rentalFee、numDaysRented 和totalRentalFee。
该类包含一个构造函数,该构造函数需要除总费用之外的所有租赁数据,总费用是根据汽车大小计算的:经济型每天 29.99 美元,中型每天 38.99 美元,全型每天 43.50 美元。该类还包括一个显示所有租赁数据的 display() 方法。
创建一个名为 LuxuryCarRental 的子类。该类(class)的租金为每天 79.99 美元。
编写一个名为 UseCarRental 的应用程序,提示用户输入租赁者的姓名、邮政编码、要租赁的汽车的大小以及他们想要租赁的天数。
您的申请应显示总租金。
确保您的应用程序同时测试 CarRental 和 LuxuryCarRental。
租车:https://pastebin.com/fq1wJF1s 豪华租车:https://pastebin.com/xBAK6NTy
package usecarrental;
public class CarRental
{
private String renterName = "";
private int rentalZipCode = 00000;
private String rentalCarSize = "";
private double rentalFeeDaily;
private int numDaysRented = 0;
private double totalRentalFee;
public CarRental(String name, int zipcode, int days, String carsize)
{
renterName = name;
rentalZipCode = zipcode;
rentalCarSize = carsize;
numDaysRented = days;
}
public void setrenterName(String name)
{
renterName = name;
}
public void setrentalZipCode(int zipcode)
{
rentalZipCode = zipcode;
}
public void setrentalCarSize(String carsize)
{
rentalCarSize = carsize;
}
public void setnumDaysRented(int days)
{
numDaysRented = days;
}
public String getrenterName()
{
return renterName;
}
public int getrentalZipCode()
{
return rentalZipCode;
}
public String getrentalCarSize()
{
return rentalCarSize;
}
public int getnumDaysRented()
{
return numDaysRented;
}
public double gettotalRentalFee()
{
return numDaysRented * rentalFeeDaily;
}
public double getrentalFeeDaily(String rentalCarSize)
{
switch (rentalCarSize)
{
case "Economy":
rentalFeeDaily = 29.99;
break;
case "Midsize":
rentalFeeDaily = 38.99;
break;
case "Fullsize":
rentalFeeDaily = 43.50;
break;
}
return rentalFeeDaily;
}
public void display()
{
System.out.println(
"Customer Details" +
"\nName = " + getrenterName() +
"\nZipcode = " + rentalZipCode +
"\nCar Size = " + getrentalCarSize() +
"\nRental Fee = " + rentalFeeDaily + "/daily" +
"\nRental Length = " + numDaysRented +
"\nTotal Fee + " + totalRentalFee
);
}
}
package usecarrental;
public class LuxuryCarRental extends CarRental
{
public LuxuryCarRental(String carsize, int days)
{
super(carsize, days);
}
public void computetotal1()
{
super.computetotal(days);
rentalFeeDaily = 79.99;
totalRentalFee = rentalFeeDaily;
}
}
最佳答案
属性和类常量:
首先,您不必在创建类属性时为其赋值。 为此目的使用构造函数。
其次,这些属性应该受到保护,因为 LuxuryCarRental
类稍后将继承它们。
您不需要 rentalFeeDaily
和 totalRentalFee
属性。
属性变为
protected String renterName;
protected int rentalZipCode;
protected String rentalCarSize;
protected int numDaysRented;
您可以从 getRentalFeeDaily()
方法获取 rentalFeeDaily
,并从 totalRentalFee()
获取 totalRentalFee
> 方法。
然后将它们放入 display()
方法中:
public void display()
{
System.out.println(
"Customer Details" +
"\nName = " + getrenterName() +
"\nZipcode = " + rentalZipCode +
"\nCar Size = " + getrentalCarSize() +
"\nRental Fee = " + getRentalFeeDaily + "/daily" +
"\nRental Length = " + numDaysRented +
"\nTotal Fee + " + totalRentalFee
);
}
最好将类中使用的常量作为类常量。
public static final double ECONOMY_FEE = 29.99;
public static final double MIDSIZE_FEE = 38.99;
public static final double FULLSIZE_FEE = 43.50;
public static final double LUXURY_FEE = 79.99;
在您的getRentalFeeDaily()
中:
public double getRentalFeeDaily() {
double rentalFeeDaily = 0;
switch (rentalCarSize) {
case "Economy":
rentalFeeDaily = ECONOMY_FEE;
break;
case "Midsize":
rentalFeeDaily = MIDSIZE_FEE;
break;
case "Fullsize":
rentalFeeDaily = FULLSIZE_FEE;
break;
case "Luxury":
rentalFeeDaily = LUXURY_FEE;
}
return rentalFeeDaily;
}
为什么要为 getRentalFeeDaily()
方法提供一个字符串参数?您的 rentalCarSize
是类中的一个属性,您可以将它用于访问此方法的对象实例。
豪华租车类
LuxuryCarRental
是一种 CarRental
,唯一改变的是费用。
您不需要 LuxuryCarRental
类的任何附加属性。
public class LuxuryCarRental extends CarRental {
public LuxuryCarRental(String name, int zipcode, int days) {
super(name, zipcode, days, "Luxury");
}
}
在创建 LuxuryCarRental
对象时,您也不必提供 rentalCarSize
字符串,因为您自己会将其作为“Luxury”提供给父类(super class)构造函数。
所以稍后,当您稍后创建 LuxuryCarRental
对象并想要计算其总费用时:
- 构造函数将提供“Luxury”作为
rentalCarSize
属性。 - 这个字符串将被赋予父类(super class)中的方法
- 该方法将使用“Luxury”
rentalCarSize
属性从getRentalFeeDaily()
获取豪华车费用后返回总费用。
UseCarRentalClass
public class UseCarRental {
public static void main(String[] args) {
CarRental cr = new CarRental("Honda", 1234, 10, "Economy");
LuxuryCarRental lcr = new LuxuryCarRental("Jaguar", 5678, 10);
System.out.println(cr.totalRentalFee());
System.out.println(lcr.totalRentalFee());
}
}
这应该足以满足您的要求。
关于Java继承LuxuryCarRental,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55287960/