gpt4 book ai didi

Java:ClassCastException

转载 作者:行者123 更新时间:2023-11-29 09:09:46 30 4
gpt4 key购买 nike

<分区>

我目前有一个项目,在那里我犯了一个错误,我无法弄清楚为什么会给我这个错误。

我的结构:

Class: Variable (commands.lib.Variable);
Class: ImageLoader extends Variable (commands.lib.Variable);

然后我将变量转换为 ImageLoader:

// There is a variable made with: Variable v = new ImageLoader();
public static Variable(commands.lib.Variable) getVariable(String value){...}
ImageLoader t = (ImageLoader)getVariable(ab[2]);

但这给了我一个错误:

Exception in thread "main" java.lang.ClassCastException: commands.lib.Variable cannot be cast to commands.variable.ImageLoader

当然,我用谷歌搜索了 ClassCastException,结果是这篇文章:Explanation of "ClassCastException" in Java

这里有人解释:

Animal a = new Dog();
Dog d = (Dog) a; // no problem, the type animal can be casted to a dog, because its a dog
Cat c = (Dog) a; // raises class cast exception, you cant cast a dog to a cat

所以对于我的代码,它看起来像这样:

Variable a = new ImageLoader();
ImageLoader b = (ImageLoader) a; // This should work, but will throw a ClassCastException
Array c = (Array) a; // This should throw a error

所以我有点无能为力。我希望有人能帮助我,并修复错误。

问候,

罗宾

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com