从超类转换子类时出现异常java.lang.classCastException

从超类转换子类时出现异常java.lang.classCastException,java,exception,casting,subclass,superclass,Java,Exception,Casting,Subclass,Superclass,当我试图使用Desguace中的方法getinfoderivida()时,标题中出现了错误: 线程“main”java.lang.ClassCastException中的异常: es.unex.cum.mdp.sesion2.Vehiculo不能被强制转换为 E.unex.cum.mdp.sesion2.Moto at es.unce.cum.mdp.sesion2.Desguace.getinfoderivida(Desguace.java:93)位于 es.unex.cum.mdp.sesi

当我试图使用
Desguace
中的方法
getinfoderivida()
时,标题中出现了错误:

线程“main”java.lang.ClassCastException中的异常: es.unex.cum.mdp.sesion2.Vehiculo不能被强制转换为 E.unex.cum.mdp.sesion2.Moto at es.unce.cum.mdp.sesion2.Desguace.getinfoderivida(Desguace.java:93)位于 es.unex.cum.mdp.sesion2.Main.Main(Main.java:21)

我不知道如何将子类
Moto
转换为从超类
Vehiculo
使用其方法
getPotentia()

谢谢

文件包es.UNU.cum.mdp.sesion2;
公共班机{
公共静态void main(字符串[]args){
人物角色p=新人物角色();
Vehiculo v=新Coche(“雷诺”,“Asd”,第1234页,“阿苏尔”);
Vehiculo v1=新摩托车(“a”,“b”,p,2345,25);
如果(v.getClass().equals(Coche.class))
System.out.println(“holi 1.0”);
脱模剂d=新脱模剂(“pepe”,2);
如果(d.addVehiculo(v))
System.out.println(“ok”);
如果(d.AddVehicleulo(v1))
System.out.println(“ok”);
字符串a=d.geta(1);
字符串b=v1.toString();
系统输出打印ln(b);
系统输出打印项次(a);
}
}
课堂脱语


您可以将
Moto
转换为
Vehiculo
,而无需检查任何内容,并且需要将其转换为
Moto
Vehiculo
的一个子类,但如果您想做相反的事情,则需要首先使用检查
Vehiculo
的实例是否是
Moto
的实例
instanceof
因为
vehicleulo
的实例不是必需的
Moto
的实例,这是出现
ClassCastException
问题的根本原因。更糟糕的是,根据您的错误消息,您试图将
Vehiculo
的实例转换为
Moto
的实例,这是不可能的

if (vehiculos[pos] instanceof Moto) {
    // Here we can safety cast to a moto
    Moto moto = (Moto) vehiculos[pos];
    ...
}
您的问题在于方法
getinfoderivida
当您到达最后一个案例时,您将其转换为
Moto
,这不可能是案例,因为我们已经知道它不是
Moto
的实例

您的代码应该是这样的:

public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    }
    if(vehiculos[pos] instanceof Coche){
        return ((Coche)vehiculos[pos]).getColor();
    } else if(vehiculos[pos] instanceof Moto){
        return String.valueOf(((Moto)vehiculos[pos]).getPotencia());
    } else if(vehiculos[pos] instanceof Camion){
        return String.valueOf(((Camion)vehiculos[pos]).getTonelaje());
    }
    throw new IllegalStateException(
        String.format("Unknown type: %s", vehiculos[pos].getClass().getName())
    );
}

更好的方法是在类
Vehiculo
中添加一个新方法
getinfoderivida
,您将为其提供一个默认实现,然后在子类上覆盖它(如果需要),您的代码将是:

public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    }
    return vehiculos[pos].getInfoDerivada();
}
公共字符串getinfoderivida(int-pos){
如果(位置<0 | |位置>=续){
返回null;
}
返回Vehicleulos[pos].getInfoDerivateDA();
}

使用
instanceof
而不是检查类是否相等,换句话说
vehiculos[pos].getClass()==Coche.class
应该是Coche.class的
vehiculos[pos]instanceof.class
它仍然返回null,我不知道为什么,似乎if’s从来都不是真的。我最后提出的是一种OO方法,所以这是一种方法,因为你的老师应该知道Java是一种OO编程语言。非常感谢你,我想我不能直接将实例分配给数组。很抱歉,我第一次在这里发布/编辑失败。
if (vehiculos[pos] instanceof Moto) {
    // Here we can safety cast to a moto
    Moto moto = (Moto) vehiculos[pos];
    ...
}
public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    }
    if(vehiculos[pos] instanceof Coche){
        return ((Coche)vehiculos[pos]).getColor();
    } else if(vehiculos[pos] instanceof Moto){
        return String.valueOf(((Moto)vehiculos[pos]).getPotencia());
    } else if(vehiculos[pos] instanceof Camion){
        return String.valueOf(((Camion)vehiculos[pos]).getTonelaje());
    }
    throw new IllegalStateException(
        String.format("Unknown type: %s", vehiculos[pos].getClass().getName())
    );
}
public boolean addVehiculo (Vehiculo v){
    ...
    if(cont<vehiculos.length){
        vehiculos[cont] = v;// instead of new Vehiculo(v);
        ...
    }
    ...
}
public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    }
    return vehiculos[pos].getInfoDerivada();
}