我想使用Vehicle类创建Car对象(即Vehicle c = new Car()),并将其正确插入列表中

时间:2015-05-02 07:25:00

标签: java inheritance polymorphism

我使用Vehicle类创建了一个Car对象(即Vehicle c = new Car()),但我的问题是如何将其插入列表然后在列表中插入后提示用户输入此对象的详细信息

import java.util.ArrayList;
import java.util.Scanner;
public class TestCar {

    public static void main(String[] args) {
        ArrayList<Car>list=new ArrayList<Car>();
        Scanner in=new Scanner(System.in);

        list.add(new Car());
         list.add(new Car("Mitsubishi",1993,5,"Pajero"));

        System.out.println("Enter Brand");
        String Brand=in.nextLine();

        System.out.println("Enter Year");
        int Year=in.nextInt();

        System.out.println("Enter Engine size");
        double Enginesize=in.nextDouble();

        System.out.println("Enter Model");
        String Model=in.nextLine();

        list.get(0).setBrand(Brand);
        list.get(0).setYear(Year);
        list.get(0).setEngineSize(Enginesize);
        list.get(0).setModel(Model);

        Veichle c = new Car(Brand, Year, Enginesize,Model); // Creating a   new object
        list.add(c); // This add will be wrong because cuz 
                     //it will tell me to change type c to car
                     // so whats the correct add?

        in.close();
    }
}

3 个答案:

答案 0 :(得分:1)

您已经创建了ArrayList<Car> - 您只能 向该列表中添加编译器知道为汽车的元素。所以你可以使用:

list.add((Car) c);

或更简单地说,只需将c的类型声明为Car即可:

Car c = new Car(...);

获取列表的值 ,您可以使用Vehicle,因为每个Car都是Vehicle

Vehicle vehicle = list.get(0);

或者,您可以将list变量更改为:

List<Vehicle> list = new ArrayList<Vehicle>();

或等效地:

List<Vehicle> list = new ArrayList<>();

(注意,通常最好使用接口(在本例中为List)作为变量类型,即使您需要在此处指定具体类(ArrayList)创建一个实例。)

但是,此时,如果setBrand仅在Car而非车辆上定义,那么修改品牌等的代码将无法运作 - 因为list.get(0)只会返回一个Vehicle。您可以使用:

Car firstVehicle = (Car) list.get(0);

...但很明显,如果您的列表实际包含非Car车辆,则会在执行时失败。

从根本上说,您需要确定您的列表是否应该能够包含任何车辆,或仅包含汽车。

顺便说一句,我强烈建议您开始遵循Java命名约定 - 使用camelCase作为本地变量(yearbrand等。)

答案 1 :(得分:1)

您应该将其创建为:

public void doWork()
    {
        db.open();
        Cursor c = db.getAllTitles();
        if(c==null)
            return;


        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
        {

          String key= c.getString(c.getColumnIndex(c.getColumnName(5)));
          //and now you can put the condition to compare current time with key

        }

    }

观察两点:

  1. 始终编程接口。
  2. 汽车永远是车辆,但反过来并非总是如此。
  3. IMO下面的代码真的很难看:

    List<Vehicle>list=new ArrayList<Vehicle>();
    

答案 2 :(得分:0)

你必须这样做。

List<vehicle> list = new new ArrayList<Vehicle>();

执行此操作后,您可以在列表

中添加车辆类型及其所有子类的对象

让我们假设:

    class Vehicle {
     //some implmentation
    }

    class Car extends Vehicle {
     //some implementation
    }

    class Truck extends Vehicle {
     // some implementation
    }

    class Main{
     public static void main(String[] args) {
      List<Vehicle> list = new ArrayList<Vehicle>();
      Car car = new Car();
      Truck truck = new Truck();
      list.add(car);
     list.add(truck)
     // you need to check the instance and cast with its appropriate class. 
      Vehicle obj = list.get(1);
      Car car1 = null;
if(obj instanceOf Car){
       car1 = (Car) obj;
}



    }
    }

或者你可以简单地这样做,然后在列表中添加你的多态对象与car。像这样。

List.add((Car) car);