-3

我应该制作一个具有MotorVehicle抽象类的程序。Car, Truck,Van是种MotorVehiclesetTerms()displayInfo()是唯一的抽象。CarString transTypeand void Car(), Vanhas int numPassengerand void Van(), as Truckhas double payLoadand void Truck().

输出应该是这样的:

品牌是马自达

车辆类型是轿车

颜色是红色

传输类型为自动

价格为 840000.0

条款是5年支付

品牌是五十铃

车辆类型是卡车

颜色是白色

有效载荷容量为 3.5

价格为 910000.0

条款是3年支付

品牌是三菱

车辆类型是家庭面包车

颜色是蓝色

乘客人数为 8

价格为 1050000.0

条款是 7 年支付

但是我的程序仍然没有产生这个输出

这是我当前的代码:

public abstract class MotorVehicle
{
    private String vehicleType;
    private String brand;
    private String color;
    private String terms;

    public MotorVehicle(String vcl, String brn, String clr, String trm)
    {
        vehicleType = vcl;
        brand = brn;
        color = clr;
        terms = trm;
    }

    public String getVehicleType()
    {
        return vehicleType;
    }

    public String getBrand()
    {
        return brand;
    }

    public String getColor()
    {
        return color;
    }

    public abstract void setTerms();

    public abstract void displayInfo();

}

//=========================================

public class Car extends MotorVehicle
{
    String transType="";
    String vehicleType;
        String brand;
    String color;
    String terms;
    int price = 0;

    public Car(String vcl, String brn, String clr, String trm)
    {
        super(vcl, brn, clr, trm);
        vehicleType = vcl;
        brand = brn;
        color = clr;
        terms = trm;
    }

    public void Car()
    {
        brand = "Mazda";
        vehicleType = "Sedan";
        color = "Red";
        transType = "Automatic";
        price = (int) (700000 + (700000*0.2));
        Double.toString(price);
        terms = "5";   
    }

    public void setTerms()
    {
         return;
    }

    public void displayInfo()
    {
        System.out.println("Brand is " + brand);
        System.out.println("Vehicle type is " + vehicleType);
        System.out.println("Color is " + color);
        System.out.println("Transmission type is " + transType);
        System.out.println("Price is " + price);
        System.out.println("Terms is " + terms + " years to pay");
    }

}

//=================================

public class Truck extends MotorVehicle
{
    double payLoad=0.0;
    String vehicleType;
        String brand;
    String color;
    String terms;
    int price = 0;

    public Car(String vcl, String brn, String clr, String trm)
    {
        super(vcl, brn, clr, trm);
        vehicleType = vcl;
        brand = brn;
        color = clr;
        terms = trm;
    }

    public void Truck()
    {
        brand = "Isuzu";
        vehicleType = "Truck";
        color = "White";
        payLoad = 3.5;
        Double.toString(payLoad);
        price = (int) (700000 + (700000*0.3));
        Double.toString(price);
        terms = "3";     
    }

    public void setTerms()
    {
         return;
    }

    public void displayInfo()
    {
        System.out.println("Brand is " + brand);
        System.out.println("Vehicle type is " + vehicleType);
        System.out.println("Color is " + color);
        System.out.println("Payload capacity is " + payLoad);
        System.out.println("Price is " + price);
        System.out.println("Terms is " + terms + " years to pay");
    }   


 }

//==========================

public class Van extends MotorVehicle
{
    int numPassenger=0;
    String vehicleType;
        String brand;
    String color;
    String terms;
    int price=0;

    public Van(String vcl, String brn, String clr, String trm)
    {
            super(vcl, brn, clr, trm);
        vehicleType = vcl;
        brand = brn;
        color = clr;
        terms = trm;
    }

    public void Van()
    {
    brand = "Mitsubishi";
    vehicleType = "Family Van";
    color = "Blue";
    numPassenger = 8;
    String.valueOf(numPassenger);
    price = (int) (700000 + (700000*0.5));
    Double.toString(price);
    terms = "7";   
}

public void setTerms()
{
     return;
}

public void displayInfo()
{
    System.out.println("Brand is " + brand);
    System.out.println("Vehicle type is " + vehicleType);
    System.out.println("Color is " + color);
    System.out.println("Number of passenger is " + numPassenger);
    System.out.println("Price is " + price);
    System.out.println("Terms is " + terms + " years to pay");
}

}

//===================

这是主程序:

public class MainVehicle 
{
    public static void main(String[] args)
    {
        BBVehicle[] vhl= new BBVehicle[3];

        int ctr=0;
        while(ctr<3)
        {
            if (ctr==0)
                vhl[ctr]=new Car();
            else if(ctr==1)
                vhl[ctr]= new Truck();
            else
                vhl[ctr]= new Van();
            vhl[ctr].displayInfo();
            ctr++;
        }
     }

}

我不确定我的程序有什么问题。请帮帮我谢谢

4

2 回答 2

2

一世)

public void Car()
public void Truck()
public void Van()

您正在定义类构造函数的返回类型

它应该是

public Car()
public Truck()
public Van()

构造函数类似于实例方法,但它与方法的不同之处在于它没有显式的返回类型,它不是隐式继承的,而且它通常对范围修饰符有不同的规则。

ii)在抽象类中定义一个无参数构造函数(来自评论)

iii)

    BBVehicle[] vhl= new BBVehicle[3];

    int ctr=0;
    while(ctr<3)
    {
        if (ctr==0)
            vhl[ctr]=new Car();

您正在将 Car 类转换为 BBVehicle 类。虽然,这两个类是不同的。这两个类之间没有关系..

于 2013-01-21T13:41:56.373 回答
1

我要给出的第一条建议是你不应该在你的 superlcas 中隐藏你的成员变量。

private在每个子类中,您声明的变量与您在超类中定义的变量同名。相反,您应该简单地从子类中删除重复项,并且:

  1. 将超类中变量的可见性更改为使它们可用于子类的东西(即protected
  2. displayInfo()利用 getter 在您的方法中访问它们。为此,您需要get[VariableName]() 在超类中设置每个字段。
于 2013-01-21T13:34:27.033 回答