我在投放时遇到了麻烦,我无法弄清楚。根据我的研究,当您将超类强制转换为类时,导致ClassCastException的原因是,它们都使用不同的类加载器。

我做了一个简单的System.out.println()检查类加载器是否不同,但是它们都打印出相同的结果@11cb01a

为了明确我要完成的任务,我正在尝试为自己创建一个简单的API来制作迷你游戏(我正在使用Bukkit API),并且在发现。首先,我有两个类,ClassCastExceptionArenaBase。我有一个名为ArenaManagerBase的类,该类扩展了ArenaManager,而一个ArenaManagerBase扩展了Arena

当我得到ArenaBaseClassCastException错误时,这是​​由于调用ArenaManager并将其转换为ArenaManager.getInstance()所致。第一次启动时,我会初始化(ArenaManager)

ArenaManager.init(this);


然后,在初始化内部,我要说几件事。

public static void init(JavaPlugin instance) {
    am = new ArenaManagerBase(); // What is returned when calling getInstance()
    am.plugin = instance;
    am.ac = new ArenaConfigBase(am.plugin);
    am.init(); // Separate initializer for any subclass that might need it.
}


引发错误的部分是这样的:

ArenaManager am = (ArenaManager) ArenaManager.getInstance();


因此,仅此而已,我就有一个要转换为的类是另一个的超类。他们似乎都具有相同的ArenaManager。我在做什么错,我该如何解决?

编辑:
这是堆栈跟踪。这很奇怪,因为在顶部显示ClassLoader,然后在底部显示null

[18:36:10 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'cast
ledefend' in plugin DefendTheCastle vAlpha
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
Caused by: java.lang.ClassCastException: me.codermusgrove.arenaapi.ArenaManagerB
ase cannot be cast to me.valdeon.defendthecastle.arena.ArenaManager
        at me.valdeon.defendthecastle.cmd.CmdCreateArena.onCommand(CmdCreateAren
a.java:27) ~[?:?]
        at me.valdeon.defendthecastle.cmd.BaseCommand.onCommand(BaseCommand.java
:30) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more

最佳答案

可以使用父类型变量来引用子类的实例。但是你不能做相反的事情。尽管可以通过显式强制转换-(ArenaManager) ArenaManager.getInstance()来绕过编译器错误检查,但最终在运行时会因CCE而失败。

在您的情况下,ArenaManagerBase是父级,而ArenaManager是子级。您拥有的实例是父实例(即new ArenaManagerBase()),并且您正在尝试将其强制转换为子实例。

10-07 22:57