Java com.sun.proxy.$proxy0 不能转换为 XXX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22587788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.sun.proxy.$proxy0 cannot be cast to XXX
提问by CrazyHenry
I was writing a RMI application. I have a method on the server side, which returns an ArrayList<User>
and User
is a class I defined myself. The code is like: list = obj.getList(); System.out.println(list.get(0).getName());
, and this line generated an exception of "com.sun.proxy.$Proxy0 cannot be cast to User". Can anybody help me about that?
我正在编写一个 RMI 应用程序。我在服务器端有一个方法,它返回一个ArrayList<User>
并且User
是我自己定义的一个类。代码如下:list = obj.getList(); System.out.println(list.get(0).getName());
,并且这一行生成了“com.sun.proxy.$Proxy0 cannot be cast to User”的异常。有人可以帮我吗?
回答by user207421
If User
is an exported remote object, as it seems to be, it appears in the client as the remote interface it implements, not as the implementation class.
IfUser
是一个导出的远程对象,就像它看起来的那样,它在客户端中作为它实现的远程接口出现,而不是作为实现类。
So that's what you must cast it to. In this case that means declaring your List as List<UserInterface>
, where UserInterface
is the name of the remote interface. Adjust to suit.
所以这就是你必须将它投射到的。在这种情况下,这意味着将您的 List 声明为List<UserInterface>
,其中UserInterface
是远程接口的名称。调整以适应。