[英]Object ArrayList For-Loop Error
我有一個對象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);
}
出現錯誤,提示“ 電動機無法解析或不在字段中 ”。
所有的類都應該允許它起作用,除非我當然缺少一個簡單的錯誤。
這是電機類:
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或Truck類,但我認為這不是問題所在。 我相信它是來自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。 當我切換到我時,出現錯誤“類型不匹配:無法從ArrayList轉換為ArrayList
ArrayList<Object> VehicleList = new ArrayList<>(Arrays.asList(vehicles));
VehicleList
被聲明為包含Object
實例,因此編譯器將只允許您訪問它在Object
所有實例上都存在的已知方法和字段。
將其更改為ArrayList<Vehicle>
。
創建一個接口IMotor
,該接口由Vehicle
類使用,並在PassCar
和其他車輛實現中實現。
IMotor.java
public interface IMotor {
public Motor getMotor();
}
馬達
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;
}
}
通行證
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());
}
}
首先,請注意命名約定。 變量應在camcelCase
中命名,例如vehicleList instead of
VehicleList`
我有一個對象ArrayList
我相信你的意思是vehicleList
聲明看起來像ArrayList<Object> vehicleList
然后會出現預期的行為,因為編譯器僅知道VehicleList.get(i)
將向您返回Object
引用。 它可以是Vehicle
,也可以是其他任何東西。 因此,它不允許您訪問motor
字段,因為Object
根本沒有這樣的字段。
將聲明更改為類似List<Vehicle> vehicleList
但是,正如其他答案所述,由於各種原因,直接訪問該領域並不是一個好主意。 一種較不那么邪惡的方法是使motor
吸氣。 (更好的方法是提供有意義的行為,而不是提供對內部數據的訪問)
您的問題是motor
不是Vehicle
類的成員,但是您正在嘗試通過Vehicle
類型的表達式(即vehicleList.get(i)
訪問它。 這是禁止的,因為編譯器無法知道每種可能的Vehicle
都有一個motor
。 畢竟,如果您添加了Bicycle
類,會發生什么?
要使此工作正常進行,您應該從Truck
和PassCar
類中刪除motor
,並將其添加到Vehicle
類中。 這樣一來, vehicleList.get(i).motor
實際上就有意義,因為可以保證Vehicle
表達式引用具有Motor
的Vehicle
。
還建議對motor
字段使用吸氣劑-也就是說,將motor
作為Vehicle
類的private
字段,並編寫方法getMotor()
來返回它。 然后,您可以編寫vehicleList.get(i).getMotor()
來獲取與列表中的一個Vehicle
關聯的Motor
對象。
多虧了您的所有注釋和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的實例。 至於在課堂上使用的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: ");
謝謝大家的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.