方法显示不正确的输出

时间:2019-03-24 16:48:47

标签: java arrays arraylist

我创建了4个班级和一个班级来测试所有内容。我想我已经为3个类正确地输入了所有代码。

在我的testVehicle中,我必须创建两个声明为Vehicle类型但实际类型为PassCar的对象。至少使一个声明类型为Vehicle但实际类型为Truck的对象。使用上面创建的实例创建一个类型为Vehicle的数组。执行一个名为show的void方法,将Vehicle数组作为唯一参数。

从已创建的数组中创建车辆ArrayList。在show方法中,使用一个foreach循环来处理Object数组。对于每个对象,显示其描述方法,然后显示toString方法。如上所述创建四个类。

请勿添加未出现在UML图中的任何属性或方法。 description()方法输出描述该类的字符串。请参阅下面有关每辆车的第一行。覆盖每个子类中的描述。不要修改任何东西。创建一个名为VehicleTest的可执行类以测试您的工作。在VehicleTest中,添加完成以下任务的代码。示例输出可以帮助您理解这些任务。

自从我习惯Java以来​​已经有一段时间了,所以我很生锈。我已经看过书,在网上寻找信息,但是我无法让我的代码显示正确的方式。

这应该是这样的:

show方法的输出。

在此应用中,乘用车是向个人注册的日常车辆:

make=Ford, model=Mustang, year=2016, price=44500.0
PassCar numPass=5, AC=true
Motor EcoBoost, cylinders=6, bhp=310, displacement=2.3

In this application, a Truck is a vehicle designed to transport cargo

make=Dodge, model=Ram, year=2016, price=46000.0
Truck type=pickup, capacity=1500
Motor Hemi, cylinders=8, bhp=707, displacement=5.7

In this application, a passenger car is an every day vehicle registered to an individual

make=Tesla, model=Model S, year=2016, price=121000.0
PassCar numPass=2, AC=true
Motor P90D, cylinders=0, bhp=762, displacement=0.0

Output from ArrayList in main

make=Ford, model=Mustang, year=2016, price=44500.0
PassCar numPass=5, AC=true
Motor EcoBoost, cylinders=6, bhp=310, displacement=2.3

make=Dodge, model=Ram, year=2016, price=46000.0
Truck type=pickup, capacity=1500
Motor Hemi, cylinders=8, bhp=707, displacement=5.7

make=Tesla, model=Model S, year=2016, price=121000.0
PassCar numPass=2, AC=true
Motor P90D, cylinders=0, bhp=762, displacement=0.







 import java.util.ArrayList;
        import java.util.Arrays;


        public class TestVehicle {

        public static void main(String[] args) {
            Motor motor1 = new Motor("EcoBoost", 6, 310, 2.3);
            Motor motor2 = new Motor("P90D",0,762,0.0);
            Motor motor3=  new Motor("Hemi", 8, 707, 5.7);

            Vehicle vehicle1=new PassCar("Ford","Mustang",2016
            ,44500.0,5,true,motor1); 

            Vehicle vehicle2=new PassCar("Tesla","Model 
            S",2016,121000.0,2,true,motor2);

            Vehicle vehicle3=new Truck("Dodge","Ram",2016,46000.0, 
              "pickup",1500,motor3);

            Vehicle[] ara = { vehicle1,vehicle2, vehicle3 };
            showVehicle(ara);

    ArrayList<Vehicle> ara2 = new ArrayList<>(Arrays.asList(ara));
            for (int i = 0; i < ara2.size(); i++) {
                System.out.println(ara2.get(i));
            }

           }

        public static void showVehicle(Vehicle[]ara) {
            for(Object i: ara){
                 System.out.println(i);



    }
        }
        }

Vehicle Class

    public class Vehicle {
        String make;
        String model;
        int year;
        double price;

        public Vehicle(String make, String model, int year, double price) {
            super();
            this.make = make;
            this.model = model;
            this.year = year;
            this.price = price;

    }   
        public void description() {
            System.out.println("In this application, a passenger car is an every day vehicle registered to an individual." );
            }

        public String toString() {
            return "Vehicle make="  + this.make + ", model=" + this.model + ", year=" + this.year + ", price=" + this.price + " " ;


        }

        }

PassCar

    public class PassCar extends Vehicle {
        int numPass;
        boolean AC;
        Motor motor;

        public PassCar(String make, String model, int year, double price, int numPass, boolean AC, Motor motor) {
            super(make, model, year, price);
            this.numPass = numPass;
            this.AC = AC;
            this.motor = motor;

    }
        @Override
        public void description() {
            System.out.println("This application, a passanger car is an every day vehicle registered to an individual");

        }


        public String toString() {
        return "numPass=" + this.numPass + "AC=" + this.AC+ "Motor=" + this.motor;
        }

        }


Truck Class

    public class Truck extends Vehicle {
        String type;
        int capacity;
        Motor motor;

        public Truck(String make, String model, int year, double price, String type, int capacity, Motor motor) {
        super(make, model, year, price);
        this.type = type;
        this.capacity = capacity;
        this.motor = motor;

    }
        @Override
        public void description() {
            System.out.println("In this application, a Truck is a vehicle designed to transport cargo");
        }


        public String toString() {
        return "Type="+ this.type+"Capacity="+ this.capacity+ "Motor=" + this.motor;

    }
    }

Motor Class

    public class Motor {
        String name;
        int cylinders;
        int bhp;
        double displacement;

        public Motor(String name, int cylinders, int bhp, double displacement) {
        this.name = name;
        this.cylinders = cylinders;
        this.bhp = bhp;
        this.displacement = displacement;

    }

        public String toString() {
        return "Motor name=" + this.name + ", cylinders=" + this.cylinders + ", bhp" + this.bhp + ", displacement" + this.displacement;

        }

        }

1 个答案:

答案 0 :(得分:0)

从输出来看,您需要为每个对象调用一次Vehicles toString()方法。但这无法完成,因为您已经覆盖了子类中的Vehicle Classes方法。因此,您必须在车辆类中定义另一个方法,然后调用该方法

class Vehicle {
        public String toString2() {
            return "Vehicle make="  + this.make + ", model=" + this.model + ", year=" + this.year + ", price=" + this.price + " " ;
        }
}

您可以使用

进行打印
public static void showVehicle(Vehicle[]ara) {
            for(Vehicle i: ara){
                 i.description();
                 System.out.println(i.toString2());
                 System.out.println(i);

            }
}

由于您在子类级别上为两种类型的车辆定义了电动机属性,因此无法使用“车辆对象”打印电动机,因此应使用instanceof方法检查并向下转换它们是哪种类型的对象

if (i instanceof PassCar){
     System.out.println(((PassCar)i).motor);
}