我有一个Object ArrayList,我需要使用 Motor 对象的 toString()方法,该方法是 Vehicle 对象。我的车辆对象位于一个ArrayList中,它使用for循环进行迭代(我知道foreach循环会更容易,但这是赋值的一部分)
以下是循环的代码:
for (int i = 0; i < VehicleList.size(); i++) {
System.out.println();
String info = VehicleList.get(i).toString();
Motor m = VehicleList.get(i).motor;
String motorInfo = m.toString();
System.out.println(info);
System.out.println(m);
}
有错误表示&#34; 电机无法解析或不是字段 &#34;。
所有课程都应该允许这项工作,除非我错过了一个简单的错误。
这是Motor类:
public class Motor {
protected String name;
protected int cylinders;
protected int bhp;
protected 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= " + name + ", cylinders= " + cylinders + ", bhp=
" + bhp + ", displacement= " + displacement;
}
}
汽车和车辆在这里被初始化(在TestVehicle类中):
//Motors
Motor EcoBoost = new Motor("EcoBoost", 6, 310, 2.3);
Motor Hemi = new Motor("Hemi", 8, 707, 5.7);
Motor P90D = new Motor("P90D", 0, 762, 0.0);
//Vehicles
Vehicle v0 = new PassCar("Ford", "Mustang", 2016, 44500.0, 5, true, EcoBoost);
Vehicle v1 = new PassCar("Tesla", "Model S", 2016, 121000.0, 2, true, P90D);
Vehicle v2= new Truck("Dodge", "Ram", 2016, 46000.0, "pickup", 1500, Hemi);
PassCar 和 Truck 是继承的Vehicle类,具有更多属性。如果需要,我可以发布PassCar或卡车类,但我不认为这是问题所在。我相信它来自For-Loop,特别是 Motor m = VehicleList.get(i).motor; 这一行,但我不知道如何修复它
车辆类:
public class Vehicle {
protected String make;
protected String model;
protected int year;
protected double price;
public Vehicle(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
public void description() {
System.out.println("Description");
}
public String toString() {
return "make= " + make + ", model= " + model + ", year= " + year +
", price= " + price;
}
}
编辑:根据分配要求,不能有任何Getter或Setter,它必须是ArrayList,而不是常规List。当我切换到我得到错误&#34;类型不匹配:无法从ArrayList转换为ArrayList
答案 0 :(得分:1)
声明ArrayList<Object> VehicleList = new ArrayList<>(Arrays.asList(vehicles));
VehicleList
包含Object
的实例,因此编译器只允许您访问它在Object
的所有实例上都知道的方法和字段。
将其更改为ArrayList<Vehicle>
。
答案 1 :(得分:0)
创建IMotor
类使用的接口Vehicle
,并在PassCar
和其他车辆实现中实现。
<强> IMotor.java 强>
public interface IMotor {
public Motor getMotor();
}
<强> Motor.java 强>
public class Motor {
protected String name;
protected int cylinders;
protected int bhp;
protected 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= " + name + ", cylinders= " + cylinders + ", bhp=" + bhp + ", displacement= " + displacement;
}
}
<强> Vehicle.java 强>
public abstract class Vehicle implements IMotor{
protected String make;
protected String model;
protected int year;
protected double price;
public Vehicle(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
public String toString() {
return "make= " + make + ", model= " + model + ", year= " + year +
", price= " + price;
}
}
<强> PassCar 强>
public class PassCar extends Vehicle{
protected Motor motor;
public PassCar(String make, String model, int year, double price, Motor motor) {
super(make, model, year, price);
this.motor = motor;
}
public Motor getMotor() {
return motor;
}
}
<强> Test.java 强>
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
Motor EcoBoost = new Motor("EcoBoost", 6, 310, 2.3);
Vehicle v0 = new PassCar("Ford", "Mustang", 2016, 44500.0, EcoBoost);
List<Vehicle> vehicles = Arrays.asList(v0);
System.out.println(vehicles.get(0).getMotor());
}
}
答案 2 :(得分:0)
首先,请注意命名约定。变量应在camcelCase
中命名,例如vehicleList instead of
VehicleList`
我有一个Object ArrayList
我认为你的意思是vehicleList
声明为ArrayList<Object> vehicleList
然后行为是预期的,因为编译器只知道VehicleList.get(i)
将返回Object
引用。它可以是Vehicle
,但它也可以是其他任何东西。因此,它不允许您访问motor
字段,因为Object
中根本没有此类字段。
将您的声明更改为List<Vehicle> vehicleList
然而,正如其他答案中所提到的,由于各种原因直接访问该字段并不是一个好主意。一种稍微不那么邪恶的方式就是拥有motor
的吸气剂。 (更好的方法是提供有意义的行为,而不是提供对内部数据的访问)
答案 3 :(得分:0)
您的问题是motor
不是Vehicle
类的成员,但您尝试通过Vehicle
类型的表达式(即vehicleList.get(i)
)来访问它。这是被禁止的,因为编译器无法知道每种可能的Vehicle
都有motor
。毕竟,如果你添加了Bicycle
类会怎么样?
要使其发挥作用,您应该从motor
和Truck
类中删除PassCar
,并将其添加到Vehicle
类。这样,vehicleList.get(i).motor
实际上是有意义的,因为Vehicle
表达式将保证引用带有Vehicle
的{{1}}。
还建议对Motor
字段使用getter,即将motor
作为motor
类的private
字段,然后编写方法Vehicle
返回它。然后,您可以编写getMotor()
以获取与列表中的一个vehicleList.get(i).getMotor()
相关联的Motor
对象。
答案 4 :(得分:-2)
感谢您的所有评论和我的Java教科书的帮助,我设法将它拼凑在一起。以下是我如何使用它:
for (int i = 0; i < vehicleList.size(); i++) {
String motorInfo = "";
String info = "";
System.out.println();
if (vehicleList.get(i) instanceof PassCar) {
info = ((PassCar)vehicleList.get(i)).toString();
**motorInfo = ((PassCar)vehicleList.get(i)).motor.toString();**
}
else if(vehicleList.get(i) instanceof Truck) {
info = ((Truck)vehicleList.get(i)).toString();
**motorInfo = ((Truck)vehicleList.get(i)).motor.toString();**
}
基本上我必须使用多态调用并检查它是否是PassCar或Truck的实例。 至于Class中使用的Array和ArrayList,我编辑它们:
Vehicle [] vehicles = new Vehicle [3];
vehicles[0] = v0;
vehicles[1] = v1;
vehicles[2] = v2;
showVehicle(vehicles);
ArrayList<Vehicle> vehicleList = new ArrayList<Vehicle>(Arrays.asList(vehicles));
System.out.println();
System.out.println("Output from ArrayList in main: ");
感谢大家的帮助!