因此,对于这项任务,我必须创建一个汽车类(父级)和一个认证的公司(子级),我需要让父类有一种方法来检查它是否仍在保修期内。 * checkWarrantyStatus()。该方法调用布尔值isCoveredUnderWarranty()来验证汽车是否仍有保修。我的问题是在认证的自有类中我必须调用isCoveredUnderWarranty()以查看它是否包含在延长保修期内,然后通过car方法中的checkWarrantyStatus()调用它。我希望这是有道理的。总而言之,我需要在子课程中检查isCoveredUnderWarranty并提供延长保修信息。然后它必须移动到父类,因此可以通过checkWarrantyStatus调用它。这是我的代码,我有1个错误。
public class Car {
public int year;
public String make;
public String model;
public int currentMiles;
public int warrantyMiles;
public int warrantyYears;
int currentYear =java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
/** construct car object with specific parameters*/
public Car (int y, String m, String mod, int mi){
this.year = y;
this.make = m;
this.model = mod;
this.currentMiles = mi;
}
public int getWarrantyMiles() {
return warrantyMiles;
}
public void setWarrantyMiles(int warrantyMiles) {
this.warrantyMiles = warrantyMiles;
}
public int getWarrantyYears() {
return warrantyYears;
}
public void setWarrantyYears(int warrantyYears) {
this.warrantyYears = warrantyYears;
}
public boolean isCoveredUnderWarranty(){
if (currentMiles < warrantyMiles){
if (currentYear < (year+ warrantyYears))
return true;
}
return false;
}
public void checkWarrantyStatus(){
if (isCoveredUnderWarranty()){
System.out.println("Your car " + year+ " " + make+ " "+ model+ " With "+
currentMiles +" is still covered under warranty");
}
else
System.out.println("Your car " + year+ " " + make+ " "+ model+ " With "+
currentMiles +" is out of warranty");
}
}
public class CertifiedPreOwnCar extends Car{
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(mi, m, mod, y);
}
public int extendedWarrantyYears;
public int extendedWarrantyMiles;
public int getExtendedWarrantyYears() {
return extendedWarrantyYears;
}
public void setExtendedWarrantyYears(int extendedWarrantyYears) {
this.extendedWarrantyYears = extendedWarrantyYears;
}
public int getExtendedWarrantyMiles() {
return extendedWarrantyMiles;
}
public void setExtendedWarrantyMiles(int extendedWarrantyMiles) {
this.extendedWarrantyMiles = extendedWarrantyMiles;
}
public boolean isCoveredUnderWarranty() {
if (currentMiles < extendedWarrantyMiles){
if (currentYear < (year+ extendedWarrantyYears))
return true;
}
return false;
}
}
public class TestCar {
public static void main(String[] args) {
Car car1 = new Car(2014, "Honda", "Civic", 255);
car1.setWarrantyMiles(60000);
car1.setWarrantyYears(5);
car1.checkWarrantyStatus();
Car car2 = new Car(2000, "Ferrari", "F355", 8500);
car2.setWarrantyMiles(20000);
car2.setWarrantyYears(7);
car2.checkWarrantyStatus();
CertifiedPreOwnCar car3 = new CertifiedPreOwnCar(2000, "Honda", "Accord", 65000);
car3.setWarrantyYears(3);
car3.setWarrantyMiles(30000);
car3.setExtendedWarrantyMiles(100000);
car3.setExtendedWarrantyYears(7);
car3.checkWarrantyStatus();
}
}
答案 0 :(得分:3)
在基类Car中,将数据成员设置为“private”。儿童无法访问其超级班的私人成员。考虑为您的数据成员使用“protected”或“public”,或者为Car类中的这些值提供getter(也包括受保护或公共)方法。
考虑以下示例:
class Parent {
private int x;
protected int y;
public int z;
}
class Child extends Parent {
public void doit(){ System.out.println(x); //compiler error }
public void doit2(){ System.out.println(y); //allowed }
public void doit3(){ System.out.println(z); //allowed }
}
编辑:
对于本帖子评论中所述的其他问题,目前CertifiedPreownCar的构造函数
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(mi, m, mod, y);
}
我相信它应该是
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(y, m, mod, mi);
}
使用您当前的变量名称很容易犯这个错误。虽然选择短变量名很有诱惑力,但它可能会引入简单的错误并使您的代码更难调试(特别是对于那些没有编写代码的人)。
考虑使用更具表现力的名称,例如'year','make','model'和'miles'