[英]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
. 让我知道这是否设计不好,但是我有两个类
User
和Recipient
。 Recipient
is a User
so it inherits User
. Recipient
是一个User
因此它继承了User
。 Recipient
has additional properties like messageId
, readDate
, keepMessage
. Recipient
具有其他属性,例如messageId
, readDate
, keepMessage
。
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.