简体   繁体   English

向下广播给出ClassCastException。 我怎样才能解决这个问题?

[英]Downcasting gives ClassCastException. How can I fix this?

I'm trying to implement a private message system. 我正在尝试实现私人消息系统。 Let me know if this is bad design but I have two classes User and Recipient . 让我知道这是否设计不好,但是我有两个类UserRecipient Recipient is a User so it inherits User . Recipient是一个User因此它继承了User Recipient has additional properties like messageId , readDate , keepMessage . Recipient具有其他属性,例如messageIdreadDatekeepMessage

My code is as follows: 我的代码如下:

//This line gives me ClassCastException
recipient = (Recipient) user;

.

 //GET id of user to send message to
String receiverId = request.getParameter("id");

//GET title of message
String title = request.getParameter("title");

//Get content of message
String content = request.getParameter("content");

//Retrieve logged in user from session
HttpSession session = request.getSession();     
User sender = (User) session.getAttribute("user");

//Instantiate a new User to hold receiver
User user = new User();
//Retrieve object of user to send message to
UserService userService = new UserService();
user = userService.getUserById(Integer.valueOf(receiverId));

//Instantiate a new Recipient (extends User)
Recipient recipient = new Recipient();
//Cast User as a Recipient
recipient = (Recipient) user;

//Instantiate a message
Message message = new Message();
//message related stuff here....

//Pass the message content and Recipient to messageService
MessageService messageService = new MessageService();
messageService.sendPrivateMessage(message, recipient);

Aside from your class hierarchy, which is another issue, you have multiple syntax problems. 除了类层次结构(这是另一个问题)之外,您还有多个语法问题。 As I pointed out you are defining the recipient variable twice, with 2 different classes. 正如我指出的那样,您使用2个不同的类两次定义了接收者变量。 Also, it looks like you are trying to cast a User to a Recipient, which will fail - a recipient is a user, but a user isn't necessarily a recipient 另外,您似乎正在尝试将用户强制转换为收件人,这将失败-收件人是用户,但用户不一定是收件人

You cannot really do that with Java. 使用Java不能真正做到这一点。 Based on what you said above you cannot cast a Recipient to a User. 根据以上所述,您无法将收件人投射给用户。 Do you really need to do this though? 您是否真的需要这样做? In the instance above it looks like you should be able to just instantiate it as a Recipient. 在上面的实例中,您似乎应该可以将其实例化为收件人。

You cannot cast a super class to a derived one. 您不能将超类转换为派生类。 Where should the additional subclass data come from? 其他子类数据应从何而来?

You can't do it with a cast. 您不能使用演员表来做。 Your best bet is to add a constructor to Recipient that takes a User argument. 最好的选择是将一个带有User参数的构造函数添加到Recipient Then you can do 那你可以做

Recipient recipient = new Recipient(user);

Either that or change sendPrivateMessage() to accept a User instead of a Recipient . 要么更改sendPrivateMessage()以接受User而不是Recipient

I think it is not good to do downcast, please check your class hierachy. 我认为不推荐您这样做是不好的,请检查您的班级等级。 if that object is really a child object, you can use dynamic_cast 如果该对象确实是子对象,则可以使用dynamic_cast

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

相关问题 ClassCastException。 如何访问基础适配器? - ClassCastException. How to access the underlying adapter? 如何在azure-activedirectory-library-for-java中修复ClassCastException? - how can I fix ClassCastException in azure-activedirectory-library-for-java? Java向下转换ClassCastException错误 - Java downcasting ClassCastException error 我的JTable抛出classCastException。 我正在从文本文件加载,需要(True / false)列显示为复选框 - My JTable throws a classCastException. I am loading from a text file and need the (True/false) column to show as Checkboxes 一个非常非常奇怪的错误ClassCastException。 PreparedStatement的setInt方法 - A very very strange error ClassCastException. PreparedStatement's setInt method 启动 Inteliji IDEA 会出错。 我该如何解决? - Launching Inteliji IDEA gives an error. How can I fix that? 为什么会收到ClassCastException以及如何解决? - Why do I get a ClassCastException and how do I fix it? 如何摆脱这个ClassCastException? (Java)的 - How can I get rid of this ClassCastException? (java) 如何修复UserDetails实现的ClassCastException? - How to fix ClassCastException for UserDetails implementation? SerializationProxy模式提供了ClassCastException:如何避免? - SerializationProxy pattern gives ClassCastException: How to avoid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM