我正在开展一项具有以下课程的任务:
Vehicle.java
(抽象类)NewVehicle.java
Vehicle.java
UsedVehicle.java
Vehicle.java
VehicleParser.java
用作解析器Drive
用作主要在VehicleParser
类中,我确定它是哪个对象。它是NewVehicle
对象或UsedVehicle
。在Drive
课程中,我使用ArrayList
个对象填充Vehicle
。
现在,当我尝试System.out.println
Arraylist
时,驱动器类只调用toString
/ UsedVehicle
中声明的NewVehicle
方法,但不调用在Vehicle.java
类中声明的方法。我首先需要它来调用Vehicle的方法toString
,然后用它toString
/ UsedVehicle
连接NewVehicle
。
以下是代码:
车辆
public abstract class Vehicle {
protected String make;
protected int modelYear;
protected String motivePower;
protected double licenseFee;
public Vehicle(String make,int modeYear,String motivePower) {
this.make = make;
this.modelYear= modeYear;
this.motivePower = motivePower;
this.licenseFee = 0.0;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
public String getMotivePower() {
return motivePower;
}
public void setMotivePower(String motivePower) {
this.motivePower = motivePower;
}
public double getLicenseFee() {
return licenseFee;
}
public void setLicenseFee(double licenseFee) {
this.licenseFee = licenseFee;
}
public abstract void computeLicenseFee();
public String toString()
{
return "\nMake:\t\t"+getMake()+
"\nModel Year:\t"+getModelYear()+
"\n Motive Power:\t"+getMotivePower()+
"\nLicense Fee:\t"+getLicenseFee();
}
public static class UsedVehicle extends Vehicle
{
public String previousLicenseState;
public int currentYear;
int yearsOld = 0;
public UsedVehicle(String make, int modelYear, String power, String previousState, int currentYear)
{
super(make,modelYear,power);
this.previousLicenseState = previousState;
this.currentYear = currentYear;
}
public String getPreviousLicenseState() {
return previousLicenseState;
}
public void setPreviousLicenseState(String previousLicenseState) {
this.previousLicenseState = previousLicenseState;
}
public int getCurrentYear() {
return currentYear;
}
public void setCurrentYear(int currentYear) {
this.currentYear = currentYear;
}
public void computeLicenseFee() {
double baseFee = 100.00;
double titleTransferFee = 15.00;
double smogWaiverFee = 0.00;
double smogAbatement = 0.00;
yearsOld = getCurrentYear() - getModelYear();
if(yearsOld > 5)
{
smogWaiverFee = 8.00;
}
if("gas".equalsIgnoreCase(getMotivePower()))
{
smogAbatement = 20.00;
}
licenseFee = baseFee + smogAbatement + titleTransferFee + smogWaiverFee;
}
public String toString()
{
return "\n Years Old:\t"+yearsOld+
"\n Previous State:\t"+getPreviousLicenseState();
}
}
public static class NewVehicle extends Vehicle
{
public double vehiclePrice;
public NewVehicle(String make, int modeYear, String motivePower,double price) {
super(make, modeYear, motivePower);
this.vehiclePrice = price;
}
public double getVehiclePrice() {
return vehiclePrice;
}
public void setVehiclePrice(double vehiclePrice) {
this.vehiclePrice = vehiclePrice;
}
public void computeLicenseFee() {
double baseFee = 150.00;
double smogAbatement = 0.00;
double priceFee = 0.00;
if("gas".equalsIgnoreCase(getMotivePower()))
{
smogAbatement = 20.0;
priceFee = getVehiclePrice()*0.15;
}
licenseFee = baseFee + smogAbatement + priceFee;
}
public String toString()
{
return "Price:\t\t$"+getVehiclePrice();
}
}
}
分析器
public class VehicleParser {
public static Vehicle parseStringToVehicle(String lineToParse)
{
Vehicle vehicleObj = null;
Vehicle.UsedVehicle usedVeh = new Vehicle.UsedVehicle(make, modelYear, power, previousState, currentYear);
return vehicleObj;
}
}
DriveClass
Vehicle obj = VehicleParser.parseStringToVehicle(inputInfo);
vehicleList.add(obj);
System.out.println(vehicleList.get(i));
答案 0 :(得分:6)
您正在覆盖toString()
方法。 Java在这里没有任何特殊的魔力。如果你想要调用超类的方法,你需要使用super
关键字明确地这样做:
@Override
public String toString()
{
return super.toString() + // Here
"\n Years Old:\t"+yearsOld+
"\n Previous State:\t"+getPreviousLicenseState();
}
答案 1 :(得分:2)
这是预期的设计,您需要从被覆盖的方法调用:
super.toString();
它将返回一个String
,您需要将其追加到被覆盖方法的返回值。例如:
return super.toString() + " is a new car!";
我可以向您推荐Apache Commons CSV吗?
答案 2 :(得分:2)
请考虑这个例子:
public class A {
public String someMethod() {
return "A method";
}
}
public class B extends A {
@Override
public String someMethod() {
return "B method";
}
}
public class C extends B {
@Override
public String someMethod() {
return "C method";
}
}
基本上这里发生的是当你继承父类时,你覆盖了父类方法中的所有内容,并且你给它提供了新的定义。通过覆盖父类类的方法,你说的是:
我给这个方法一个新的定义。从现在开始,对于我的所有对象和我孩子的对象,这只是将被考虑的定义,并且父方法的任何定义都是无效的。
现在,如果您希望在调用此方法定义之前调用父级的方法定义,那么您必须在代码中使用super.methodName()
明确说明。
public class A {
public String someMethod() {
return "A method";
}
}
public class B extends A {
@Override
public String someMethod() {
return super.someMethod() + "B method";
}
}
public class C extends B {
@Override
public String someMethod() {
return super.someMethod() + "C method";
}
}
答案 3 :(得分:0)
当您调用子类方法时,将调用覆盖的方法,并且将覆盖父方法中的所有定义,并且您将仅获得覆盖的方法定义。因此,要使用父母的方法定义,还需要在子类方法中使用super()
方法...
return super.toString() + " is a new car!";