[英]How to apply polymorphism in this object
I have the following class 我有以下课程
class Message{
private List content;
private String messageType;
public Message(String messageType, List content){
this.content = content;
this.messageType = messageType;
}
}
Okey, now imagine i want to send different message types. Okey,现在想象我想发送不同的消息类型。 For example, the message type "friends". 例如,消息类型为“ friends”。 I want to store their ID number ( int
for example) as a key to find their names ( String
). 我想将其ID号(例如int
)存储为查找其名称( String
)的键。 That reminds me that i should use the HashMap
class. 那使我想起了我应该使用HashMap
类。 Or a simple String
, whatever. 或简单的String
,随便。 And other more cases where i need to use other object types. 还有其他需要使用其他对象类型的情况。
The main question here is: how i should proceed here to code a class that have two attributes: 这里的主要问题是:我应该如何在此处编写具有两个属性的类:
I have read that casting is a bad practice, so i dont want to declare content
as an object and then, cast it in function of the message type. 我已经读过,强制转换是一种不好的做法,所以我不想将content
声明为对象,然后将其强制转换为消息类型的函数。
My thoughts: 我的想法:
getContent()
in Message
class, i need to stablish the data type that it returns, what get me back to the main issue. 我没有得到的是,如果我在Message
类中声明了类似getContent()
的抽象方法,则需要稳定返回的数据类型,这使我回到了主要问题。 This way get useless since i need to send the Message
and not the subclass. 因为我需要发送Message
而不是子类,所以这种方法变得无用。 You don't need messageType
at all, you can just define a generic type with class and use it in content
, eg: 您根本不需要messageType
,只需定义带有类的泛型类型并将其用于content
,例如:
class Message<T> {
private List<T> content;
public Message(List<T> content){
this.content = content;
}
}
Now, let's say if the type is String
, you can do: 现在,假设类型为String
,则可以执行以下操作:
Message<String> message = new Message<>(new ArrayList<String>());
This way, you can instantiate Message
class with different types. 这样,您可以实例化具有不同类型的Message
类。 Here is the documentation on generic class types. 这是有关通用类类型的文档。
class Message{
private List content;
private HashMap<String,Integer> messageType = new HashMap<String,Integer>();
public Message(HashMap messageType, List content){
this.content = content;
this.messageType = messageType;
}
}
Does this code make sense??? 这段代码有意义吗???
You can have an interface that contains the common methods that apply to all message types and the have all the messages implement the Sendable interface. 您可以具有一个接口,该接口包含适用于所有消息类型的通用方法,并使所有消息实现Sendable接口。
interface Sendable {
public String getContent();
public MessageType getMessageType();
}
public FriendMessage implements Sendable {
private String title;
private Map friends;
private messageType = MessageType.Friends;
public String getContent(){
return title+ friends.toString();
}
public MessageType getMessageType() {
return this.messageType;
}
}
public Enum MessageType {
Friends, NoFriends, Lonely
}
You should also have an Enum that will classify all your message types. 您还应该具有一个将所有消息类型分类的枚举。 This will allow you to determine the message type without having to do instanceof
or string comparison. 这将使您无需执行instanceof
或字符串比较即可确定消息类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.