我想使用Vehicle类(I)创建Car对象。e车辆c =新车(),正确插入列表

时间:2022-12-24 21:45:43

I created a Car object using Vehicle class ( i.e Vehicle c = new Car() ), but my problem is how I will insert it into the list then Prompt the user to enter details for this object after the insertion in the list

我使用Vehicle类(I)创建了一个Car对象。e 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 个解决方案

#1


1  

You've created an ArrayList<Car> - you can only add elements to that list that the compiler knows to be cars. So you could use:

您已经创建了ArrayList -您只能向编译器知道是cars的列表中添加元素。所以你可以使用:

list.add((Car) c);

or more simply, just declare the type of c to be Car to start with:

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

Car c = new Car(...);

Getting values out of the list, you're fine to use Vehicle, because every Car is a Vehicle:

从列表中获取值,你可以使用汽车,因为每辆车都是一辆车:

Vehicle vehicle = list.get(0);

Alternatively, you could change your list variable to:

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

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

Or equivalently:

或者说:

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

(Note that it's generally preferrably to use the interface (List in this case) for the variable type, even though you need to specify a concrete class (ArrayList here) to create an instance.)

(请注意,尽管您需要指定一个具体类(这里的ArrayList)来创建实例,但是使用该变量类型的接口(在本例中是List)通常是可取的。

At this point, however, your code that modifies the brand etc wouldn't work if setBrand is only defined on Car, not vehicle - because list.get(0) would only return a Vehicle. You could potentially use:

然而,此时,如果setBrand只定义在汽车上,而不是在车辆上,那么修改品牌等的代码就不能工作——因为list.get(0)只返回一辆汽车。您可以使用:

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

... but obviously that would fail (at execution-time) if your list actually contained a non-Car vehicle.

…但很明显,如果你的清单中实际上包含了一辆非汽车,那就会失败(在执行阶段)。

Fundamentally, you need to decide whether your list should be able to contain any vehicle, or only cars.

从根本上说,您需要决定您的列表应该包含任何车辆,还是只包含车辆。

As an aside, I'd strongly recommend that you start following Java naming conventions - use camelCase for your local variables (year, brand etc.)

顺便提一下,我强烈建议您开始遵循Java命名约定——对本地变量(年份、品牌等)使用camelCase。

#2


1  

You should have created it as:

你应该把它写成:

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

Two point to observe:

两个观察:

  1. Always program to interface.
  2. 总程序界面。
  3. A car is always a vehicle but reverse is not always true.
  4. 汽车永远是一种交通工具,但倒车并不总是正确的。

Also IMO the following code is really ugly:

另外,下面的代码非常难看:

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

#3


0  

you have to do this.

你必须这么做。

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

after doing this, you can add vehicle-type, and objects of all its sub-classes in list

这样做之后,您可以在列表中添加车辆类型和所有子类的对象

lets suppose:

让我们假设:

    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;
}



    }
    }

or you can simply do this, before adding in list cast your polymorphic object with car. like this.

或者,你可以简单地这样做,在添加列表之前,用car抛出多态对象。像这样。

List.add((Car) car);

#1


1  

You've created an ArrayList<Car> - you can only add elements to that list that the compiler knows to be cars. So you could use:

您已经创建了ArrayList -您只能向编译器知道是cars的列表中添加元素。所以你可以使用:

list.add((Car) c);

or more simply, just declare the type of c to be Car to start with:

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

Car c = new Car(...);

Getting values out of the list, you're fine to use Vehicle, because every Car is a Vehicle:

从列表中获取值,你可以使用汽车,因为每辆车都是一辆车:

Vehicle vehicle = list.get(0);

Alternatively, you could change your list variable to:

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

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

Or equivalently:

或者说:

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

(Note that it's generally preferrably to use the interface (List in this case) for the variable type, even though you need to specify a concrete class (ArrayList here) to create an instance.)

(请注意,尽管您需要指定一个具体类(这里的ArrayList)来创建实例,但是使用该变量类型的接口(在本例中是List)通常是可取的。

At this point, however, your code that modifies the brand etc wouldn't work if setBrand is only defined on Car, not vehicle - because list.get(0) would only return a Vehicle. You could potentially use:

然而,此时,如果setBrand只定义在汽车上,而不是在车辆上,那么修改品牌等的代码就不能工作——因为list.get(0)只返回一辆汽车。您可以使用:

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

... but obviously that would fail (at execution-time) if your list actually contained a non-Car vehicle.

…但很明显,如果你的清单中实际上包含了一辆非汽车,那就会失败(在执行阶段)。

Fundamentally, you need to decide whether your list should be able to contain any vehicle, or only cars.

从根本上说,您需要决定您的列表应该包含任何车辆,还是只包含车辆。

As an aside, I'd strongly recommend that you start following Java naming conventions - use camelCase for your local variables (year, brand etc.)

顺便提一下,我强烈建议您开始遵循Java命名约定——对本地变量(年份、品牌等)使用camelCase。

#2


1  

You should have created it as:

你应该把它写成:

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

Two point to observe:

两个观察:

  1. Always program to interface.
  2. 总程序界面。
  3. A car is always a vehicle but reverse is not always true.
  4. 汽车永远是一种交通工具,但倒车并不总是正确的。

Also IMO the following code is really ugly:

另外,下面的代码非常难看:

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

#3


0  

you have to do this.

你必须这么做。

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

after doing this, you can add vehicle-type, and objects of all its sub-classes in list

这样做之后,您可以在列表中添加车辆类型和所有子类的对象

lets suppose:

让我们假设:

    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;
}



    }
    }

or you can simply do this, before adding in list cast your polymorphic object with car. like this.

或者,你可以简单地这样做,在添加列表之前,用car抛出多态对象。像这样。

List.add((Car) car);