![](/img/trans.png)
[英]Exception in thread “main” java.lang.ClassCastException:
[英]Exception in thread "main" error during object casting [java.lang.ClassCastException] - How to fix casting issue?
我正在編寫一個由多個類組成的程序(每個類有 2 個超類和幾個子類)。 在我的驅動程序文件中,我創建了兩個 arrays:第一個數組包含來自兩個完全獨立的類的對象(與每個類無關,沒有父/子類關系),第二個數組包含來自我的一個超類及其子類的對象。
在編寫和測試從每個 arrays 中查找和打印最便宜和最昂貴的對象的方法時,我遇到了一個與我的轉換相關的錯誤,我必須這樣做才能使該方法起作用。 我在下面包含了我的各種課程的較短版本:
//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())
我的代碼 go 到底哪里錯了? 我的轉換語法是錯誤的,還是我需要解決更深層次的問題?
您正在通過if(mixedObjects[i] instanceof Boat)
Boat
object 是否是 Boat 的實例,但您當然不知道mixedObjects[max]
是Boat
的類型。 最小情況也是如此。 您也可以添加一個額外的條件,例如mixedObjects[max] instanceof Boat
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.