有人可以看看我的代码,并建议我在工作代码的路上徘徊。
作业如下: 编写一个名为UseLoan的应用程序,它使用名为PersonalLoan和子类的抽象类来显示两种不同类型的贷款 - 家庭和汽车 - 以及每种贷款的每月成本。每个子类包含一个构造函数,该构造函数在提示用户输入成本确定决策中使用的至少一个数据输入项后,根据贷款类型设置每月成本。 (例如,通过汽车贷款,您可能会询问汽车的年龄,或者它是否是跑车。)在PersonalLoan类中包含一个抽象的toString()方法,该方法构造一个包含所有相关数据的String。提示用户输入保险类型,然后创建并显示相应的对象。将文件保存为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;
}
}
这里是班级主页:
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());
}
}
}
答案 0 :(得分:1)
Uncompilable source code - CarLoan is not abstract and does not override abstract method setPrice5() in PersonalLoan
编译器错误消息很明确。您尚未在setPrice5()
课程中为PersonalLoan
提供实施。
与setPrice5()
类中的HomeLoan
方法类似,您还需要将setPrice5()方法添加到PersonalLoan
类。
答案 1 :(得分:0)
堆栈跟踪的基本问题是CarLoan未在setPrice5()
类中实现PersonalLoan
。
但是按照设计你的课程是不对的
PersonalLoan是独立的贷款类型,因此不应该有像setSportsPrice等贷款特定信息
PersonalLoan
应该有类似的方法
1)setLoanAmount()
- 摘要,
2)setLoanAmount()
3)setEMI()
- 摘要
4)getEMI()
它们是根据赋值语句中提到的输入参数在子类的构造函数中设置的
public HomeLoan(double loanAmount, int loanTenure) {
super(title);
setLoanAmount(loanAmount)
int homeLoanRate = 10;
double emi = (loanAmount*loanTenure*homeLoanRate)/100; // pls change formula. This is a just a guideline
setEMI(emi);
}
同样CarLoan
应该采用当前所有者的购买成本,汽车年龄,任期等参数,并根据折旧计算当前价格,从而计算emi
希望这有帮助