有人可以看看我的代码并建议我在工作代码的路径上徘徊的地方。
作业内容如下: 编写一个名为 UseLoan 的应用程序,该应用程序使用一个名为 PersonalLoan 的抽象类和子类来显示两种不同类型的贷款——房屋和汽车——以及每种贷款的每月费用。每个子类都包含一个构造函数,该构造函数在提示用户输入至少一个用于成本确定决策的数据输入项后,根据贷款类型设置每月成本。(例如,对于汽车贷款,您可能会询问汽车的年龄,或者它是否是一辆跑车。)在 PersonalLoan 类中包含一个抽象的 toString() 方法,该方法构造一个包含所有相关数据的字符串。提示用户选择保险类型,然后创建并显示相应的对象。将文件保存为 PersonalLoan.java、CarLoan.java、homeLoan.java 和 UseLoan.java。
当我编译代码时出现以下错误
线程“main”java.lang.Error 中的异常:未解决的编译问题:无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 HomeLoan
at UseLoan.main(UseLoan.java:7)
到目前为止,我的代码如下:
public abstract class PersonalLoan {
String title = new String();
double sportsPrice, suvPrice, hybridPrice, pickupPrice, price30, price15, price5;
public PersonalLoan(String t) {
title = t;
}
public String getTitle() {
return title;
}
public double getSportsPrice() {
return sportsPrice;
}
public abstract void setSportsPrice();
public double getSUVPrice() {
return suvPrice;
}
public abstract void setSUVPrice();
public double getHybridPrice() {
return hybridPrice;
}
public abstract void setHybridPrice();
public double getPickupPrice() {
return pickupPrice;
}
public abstract void setPickupPrice();
public double getPrice30() {
return price30;
}
public abstract void setPrice30();
public double getPrice15() {
return price15;
}
public abstract void setPrice15();
public double getPrice5() {
return price5;
}
public abstract void setPrice5();
}
这是第二个子类 PersonalLoan:
public class CarLoan extends PersonalLoan {
public CarLoan(String title) {
super(title);
setSportsPrice();
setSUVPrice();
setHybridPrice();
setPickupPrice();
}
public void setSportsPrice() {
super.sportsPrice = 1.99;
}
public void setSUVPrice() {
super.suvPrice = 2.99;
}
public void setHybridPrice() {
super.hybridPrice = 3.99;
}
public void setPickupPrice() {
super.pickupPrice = 4.99;
}
}
这是 HomeLoan 类:
public class HomeLoan extends PersonalLoan {
public HomeLoan(String title) {
super(title);
setPrice30();
setPrice15();
setPrice5();
}
public void setPrice30() {
super.price30 = 1.99;
}
public void setPrice15() {
super.price15 = 2.99;
}
public void setPrice5() {
super.price5 = 3.99;
}
}
这是主类 UseLoan:
public class UseLoan {
public static void main(String[] args) {
PersonalLoan aPersonalLoan[] = new PersonalLoan[5];
int x;
aPersonalLoan[0] = new CarLoan("Sports Car");
aPersonalLoan[1] = new CarLoan("SUV");
aPersonalLoan[2] = new CarLoan("Hybrid");
aPersonalLoan[3] = new CarLoan("Pickup Truck");
aPersonalLoan[5] = new HomeLoan("30 Year Mortgage");
for (x = 0; x < aPersonalLoan.length; ++x) {
System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getSportsPrice());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getSUVPrice());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getHybridPrice());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getPickupPrice());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[X].getPrice30());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[X].getPrice15());
//System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[X].getPrice5());
}
}
}