简体   繁体   English

Java向下转换ClassCastException错误

[英]Java downcasting ClassCastException error

I got tasked with a bug and got stuck with a problem with I guess downcasting in Java. 我承担了一个错误的任务,并且陷入了Java向下转换的问题。 Here's the code: 这是代码:

public class ComItem {

private String someValue1;
private String someValue2;

public void copy(ComItem temp) {
    this.someValue = temp.someValue1;
    this.someValue2 = temp.someValue2;
}

... getters & setters & etc omitted
}


public class ItemA extends ComItem {

private ThingA thingA;

@Override
public void copy(ComItem temp) {
    super.copy(temp);
    this.thingA = ((ItemA) temp).thingA; // <- **ERROR**
}

... getters & setters & etc omitted
}

The program throws ClassCastException when I try to use ItemA's copy(..) here: 当我尝试在此处使用ItemA的copy(..)时,程序将引发ClassCastException:

if (form.getTempObject() instanceof ComItem) {
    ...
    ComItem temp = (ComItem) form.getTempObject();
    ComItem itemToEdit = (ComItem) form.getCurrentEditedObject(); // currentEditedObject is ItemA class
    itemToEdit.copy(temp);
    ...
} 

There are many classes like ItemA similar in structure, each with its own Thingx fields. 有许多类似ItemA的类,它们的结构相似,每个类都有自己的Thingx字段。

Could anyone please help me with a solution to how to change it to make it work? 有人可以帮我解决如何更改它以使其正常工作的解决方案吗?

In your code you checked types in the class that use the hierarchy 在代码中,您检查了使用层次结构的类中的类型

if (form.getTempObject() instanceof ComItem) {

but the tempObject could be any subclass of ComItem (or even ComItem if not abstract). 但是tempObject可以是ComItem的任何子类(如果不是抽象的话,甚至可以是ComItem)。

So in your copy method you supose that always use ItemA (and haven't checked it type), and not always is true. 因此,在您的复制方法中,您假设始终使用ItemA(并且尚未检查其类型),但并非总是如此。

I don't like instanceOf because is not so OO, but in this case you may use it. 我不喜欢instanceOf,因为它不是OO,但是在这种情况下,您可以使用它。

@Override
public void copy(ComItem temp) {
    super.copy(temp);
    if (form.getTempObject() instanceof ItemA) {
        this.thingA = ((ItemA) temp).thingA; // <- **NOW NO ERROR**
    }
}

Or another alterative, its that ComItem implements an interface Copyable 或另一个替代方案,ComItem实现一个接口Copyable

public interface Copyable {

copyFrom(ItemA toCopy);
copyFrom(ItemB toCopy);
copyFrom(ItemC toCopy);
....
}

and you can define in any class the way they have to copy from (it's more polimorfic and OO). 并且您可以在任何类中定义它们必须从中进行复制的方式(这是更复杂的和面向对象的)。

Is form.getTempObject() also returning an instance of ItemA class? form.getTempObject()还返回ItemA类的实例?
That is the main requirement for you to be able to pass it to ItemA.copy() method and be able to cast it inside, to an ItemA . 这是主要的需求,为您能够将它传递给ItemA.copy()方法,并能够里面丢,一个ItemA

Just because itemToEdit is an instance of ItemA does not guarantee the argument passed to copy() method is also an ItemA . 正因为itemToEdit是实例ItemA不保证传递给参数copy()方法也是ItemA
itemToEdit being an instance of ItemA causes the ItemA.copy() method to be called instead of ComItem.copy() . itemToEdit是实例ItemA使ItemA.copy()被调用,而不是方法ComItem.copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM