在抽象类中使用构造函数的任何方法?

我的抽象类代码:

public abstract class CVehicle {

//abstract variables
abstract String getManufacturer();

abstract int getYear();

abstract double getPrice();

abstract String getStatus();

abstract double getWeight();

//constructors
public CVehicle()
{
    getManufacturer();
    getYear();
    getPrice();
    getStatus();
    getWeight();
}

public CVehicle(String getManufacturer,int getYear,double getPrice,String getStatus,double getWeight)
{
    getManufacturer();
    getYear();
    getPrice();
    getStatus();
    getWeight();
}

我还有两个不同的类来扩展当前的类,这是一个类,另一个实际上是相同的。

public class Digger extends CVehicle
{
    String strManufacturer = "dftManufacturer";
    int intYear = 2019;
    double dblPrice = 0;
    String strStatus = "dftStatus";
    double dblWeight = 0;

double dblArmLength = 0;
String strTrackType = "dftTrackType";

public String getManufacturer()
{
    return strManufacturer;
}
public void setManufacturer(String strNewManufacturer)
{
    strManufacturer = strNewManufacturer;
}

public int getYear() 
{
    return intYear;
}
public void setYear(int intNewYear)
{
    intYear = intNewYear;
}

public double getPrice()
{
    return dblPrice;
}
public void setPrice(double dblNewPrice)
{
    dblPrice = dblNewPrice;
}

public String getStatus()
{
    return strStatus;
}
public void setStatus(String strNewStatus)
{
    strStatus = strNewStatus;
}

public double getWeight()
{
    return dblWeight;
}
public void setWeight(double dblNewWeight)
{
    dblWeight = dblNewWeight;
}

public double getarmLength()
{
    return dblArmLength;
}
public void setarmLength(double dblNewArmLength)
{
    dblArmLength = dblNewArmLength;
}

public String getTrackType()
{
    return strTrackType;
}
public void setTrackType(String strNewTrackType)
{
    strTrackType = strNewTrackType;
}

Digger(String stNewManufacturer,int intNewYear,double dblNewPrice,String strNewStatus,double dblNewWeight)
{
    super();
    dblArmLength = 0;
    strTrackType = "";
}

public Digger(String strNewManufacturer,double dblNewWeight,double dblNewArmLength,String strNewTrackType)
{
    super();
    dblArmLength = dblNewArmLength;
    strTrackType = strNewTrackType;
}

因此,使用digger构造函数将添加的两个新变量ArmLength和TrackType都成功地放入了数组,但是从抽象类获取的抽象变量并未被覆盖,它们只是在此处显示的输入部分中设置为默认值:

static void acceptDiggerDetails() throws Exception{
    //temporary variables for input
    String strTempManufacturer = "dftManufacturer";
    int intTempYear = 2019;
    double dblTempPrice = 0;
    String strTempStatus = "dftStatus";
    double dblTempWeight = 0;

    double dblTempArmLength = 0;
    String strTempTrackType = "dftTrackType";

    //scanner to take input data
    Scanner sc = new Scanner(System.in);

    //accepting information
    System.out.print("Enter Manufacturer: ");
    strTempManufacturer = sc.nextLine();

    System.out.print("Enter Year: ");
    intTempYear = sc.nextInt();
    sc.nextLine();

    System.out.print("Enter Price: ");
    dblTempPrice = sc.nextDouble();
    sc.nextLine();

    System.out.print("Enter Status: ");
    strTempStatus = sc.nextLine();

    System.out.print("Enter Weight: ");
    dblTempWeight = sc.nextDouble();
    sc.nextLine();

    System.out.print("Enter Arm Length: ");
    dblTempArmLength = sc.nextDouble();
    sc.nextLine();

    System.out.print("Enter Track Type: ");
    strTempTrackType = sc.nextLine();

    DiggerList[intDiggerCount] = new Digger(strTempManufacturer,intTempYear,dblTempPrice,strTempStatus,dblTempWeight,dblTempArmLength,strTempTrackType);

    intDiggerCount++;
    System.out.print("Information accepted,returning to main menu. ");
}

因此,显然将其设置为我提供的默认值会被覆盖,但是当我有用户输入数据时不会覆盖它们。

h3251300 回答:在抽象类中使用构造函数的任何方法?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2918376.html

大家都在问