我正在撰寫一個由幾個類組成的程式(每個類有 2 個超類和幾個子類)。在我的驅動程式檔案中,我創建了兩個陣列:第一個陣列包含來自兩個完全獨立的類的物件(與每個類無關,沒有父/子類關系),第二個陣列包含來自我的一個超類及其子類的物件。
在撰寫和測驗從每個陣列中查找和列印最便宜和最昂貴的物件的方法時,我遇到了一個與我的轉換有關的錯誤,我必須這樣做才能使該方法作業。我在下面包含了我的各種課程的較短版本:
//First superclass
package Plane;
public class Plane {
private String brand;
private double price;
private int horsepower;
public Plane() {
brand = "test";
price = 50000.99;
horsepower = 500;
}
public Plane(String planeBrand, double planePrice, int planePower) {
this.brand = planeBrand;
this.price = planePrice;
this.horsepower = planePower;
}
public String getBrand() {
return this.brand;
}
public void setBrand(String planeBrand) {
this.brand = planeBrand;
}
public double getPrice() {
return this.price;
}
public void setPrice(double planePrice) {
this.price = planePrice;
}
public int getPower() {
return this.horsepower;
}
public void setPower(int planePower) {
this.horsepower = planePower;
}
public Airplane(Plane plane) {
this.brand = plane.getBrand();
this.price = plane.getPrice();
this.horsepower = plane.getPower();
}
public String toString() {
return "The airplane is manufactured by " this.brand " and costs $" this.price ". It has " this.horsepower " horsepower.";
}
public boolean equals(Plane plane) {
if (!(plane instanceof Plane) || plane == null) {
return false;
} else if (this.brand != plane.getBrand() || this.price != plane.getPrice() || this.horsepower != plane.getPower()) {
return false;
} else {
return true;
}
}
}
//Second superclass
package Boat;
public class Boat {
private double weight;
private double price;
public UAV() {
weight = 3949.5;
price = 64000;
}
public UAV(double boatWeight, double boatPrice) {
weight = boatWeight;
price = boatPrice;
}
public double getWeight() {
return this.weight;
}
public void setWeight(double boatWeight) {
this.weight = boatWeight;
}
public double getPrice() {
return this.price;
}
public void setPrice(double boatPrice) {
this.price = boatPrice;
}
public Boat(Boat boat) {
this.weight = boat.getWeight();
this.price = boat.getPrice();
}
public String toString() {
return "This boat weighs " this.getWeight() "kg and costs $" this.getPrice() ".";
}
public boolean equals(Boat boat) {
if (!(boat instanceof Boat) || boat == null) {
return false;
} else if (this.price != boat.price || this.weight != boat.weight) {
return false;
} else {
return true;
}
}
}
//Driver file that produces the error
public class Main {
public static void main(String[] args) {
Object[] mixedObjects = new Object[8];
mixedObjects[0] = new Plane();
mixedObjects[1] = new Plane();
mixedObjects[2] = new Helicopter();
mixedObjects[3] = new Helicopter();
mixedObjects[4] = new Drone();
mixedObjects[5] = new Drone();
mixedObjects[6] = new Boat();
mixedObjects[7] = new Boat();
Object[] planeObjects = new Object[6];
airplaneObjects[0] = new Plane();
airplaneObjects[1] = new Plane();
airplaneObjects[2] = new Helicopter();
airplaneObjects[3] = new Helicopter();
airplaneObjects[4] = new Drone();
airplaneObjects[5] = new Drone();
findLeastAndMostExpensiveBoat(mixedObjects);
findLeastAndMostExpensiveBoat(planeObjects);
//The top line is line 91 (SEE ERROR MESSAGE)
public static void findLeastAndMostExpensiveBoat(Object[] mixedObjects) {
if(mixedObjects.length == 1 && mixedObjects[0] instanceof Boat) {
System.out.println(mixedObjects[0] " is the most and least expensive.");
}
else if(mixedObjects.length == 0) {
System.out.println("Empty array");
}
else if (mixedObjects.length == 1 && !(mixedObjects[0] instanceof Boat)){
System.out.println("No UAV object detected.");
}
else {
int max = 0;
int min = 0;
//Maximum
for(int i = 0 ; i< mixedObjects.length; i ) {
if(mixedObjects[i] instanceof Boat) {
System.out.println(mixedObjects[i].getClass());
//The following line is line 157 (SEE ERROR MESSAGE)
if(((Boat)mixedObjects[i]).getPrice() > ((Boat)mixedObjects[max]).getPrice()) {
max = i;
}
} else {
System.out.println("No Boat object detected.");
}
}
//Mininmum
for(int i = 0 ; i< mixedObjects.length; i ) {
if(mixedObjects[i] instanceof Boat) {
if(((Boat)mixedObjects[i]).getPrice() < ((Boat)mixedObjects[min]).getPrice()) {
min = i;
}
} else {
System.out.println("No Boat object detected.");
}
}
}
}
}
findLeastAndMostExpensiveBoat方法基本上檢查任何陣列中最昂貴和最便宜的船。如果沒有船,那么它會列印一條訊息說是這樣。運行代碼時得到的錯誤代碼是:
Exception in thread "main" java.lang.ClassCastException: class Plane.Plane cannot be cast to class Boat.Boat (Plane.Plane and Boat.Boat are in unnamed module of loader 'app')
at Main.findLeastAndMostExpensiveBoat(Main.java:157)
at Main.main(Main.java:91)
根據錯誤訊息的第 91 行是:findLeastAndMostExpensiveBoat(planeObjects);
根據錯誤訊息的第 157 行是:if(((Boat)mixedObjects[i]).getPrice() > ((Boat)mixedObjects[max]).getPrice())
我的代碼到底哪里出錯了?我的演員表語法錯誤,還是我需要解決更深層次的問題?
uj5u.com熱心網友回復:
您正在檢查一個物件是否是Boatby的實體,if(mixedObjects[i] instanceof Boat)但您肯定不知道它mixedObjects[max]是 的型別Boat。對于 min 情況也是如此。您也可以添加一個額外的條件mixedObjects[max] instanceof Boat。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/429532.html
