反射:将对象转换为子类而不使用instanceof
我有这个简单的接口/类:
public abstract class Message {}
public class Message1 extends Message {}
public class Message2 extends Message {}
和一个实用程序类:
public class Utility {
public void handler(Message m) {
System.out.println("Interface: Message");
}
public void handler(Message1 m) {
System.out.println("Class: Message1");
}
public void handler(Message2 m) {
System.out.println("Class: Message2");
}
}
现在,主类:
public static void main(String[] args) {
Utility p = new Utility();
Message1 m1 = new Message1();
p.handler(m1);
Message m = (Message) m1;
p.handler(m);
}
输出是
> Class: Message1
> Interface: Message
我希望 p.handler(m)
调用方法 p.handler(m: Message1)
我不想使用“手动”命令 instanceof
因为我有很多情况:
if(m instance of Message1)
p.handler((Message1)m)
else if (m instanceof Message2)
p.handler((Message2)m)
...
如果我调用 m.getClass()
我获得“mypackage .Message1”,因此是子类而不是超类。
我尝试使用此代码(使用反射):
p.handler(m.getClass().cast(m));
但输出是
> Interface: Message
所以,这是我的问题。我会在运行时将超类对象转换为子类对象,而不使用“代码命令”istanceof。
我想要一个像这样的正确命令:
p.handler((m.getclass)m);
我怎样才能获得它?有可能吗?
I have this simple interface/class:
public abstract class Message {}
public class Message1 extends Message {}
public class Message2 extends Message {}
And an utility class:
public class Utility {
public void handler(Message m) {
System.out.println("Interface: Message");
}
public void handler(Message1 m) {
System.out.println("Class: Message1");
}
public void handler(Message2 m) {
System.out.println("Class: Message2");
}
}
Now, the main class:
public static void main(String[] args) {
Utility p = new Utility();
Message1 m1 = new Message1();
p.handler(m1);
Message m = (Message) m1;
p.handler(m);
}
The output is
> Class: Message1
> Interface: Message
I would that p.handler(m)
call the method p.handler(m:Message1)
I don't want use the "manual" command instanceof
because I have many cases:
if(m instance of Message1)
p.handler((Message1)m)
else if (m instanceof Message2)
p.handler((Message2)m)
...
If I call m.getClass()
I obtain "mypackage.Message1", so the subclass and not the superclass.
I try with this code (use reflection):
p.handler(m.getClass().cast(m));
But the output is
> Interface: Message
So, this is my problem. I would do a runtime cast of superclass object to subclassobject without use the "code command" istanceof.
I would a right command like this:
p.handler((m.getclass)m);
How can I obtain it? It's possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 将根据编译时已知的信息来调用该方法。您可以做的就是向接口添加一个方法,该方法调用对象的正确处理程序方法。
您对处理程序的调用变为:
现在调用 Utility::handler(Message1),即使 main 中的引用属于 Message 接口的类型。
Java will call the method on the basis of information known at compile time. What you could do is add a method to the interface that calls the correct handler method for the object.
Your calls to the handler become:
which now calls Utility::handler(Message1) even though the reference in main is of type of the Message interface.
我不确定你的handle(..)方法应该做什么,但由于它们依赖于Message的特定实例,你应该考虑将handle()方法添加到你的Message类中(作为抽象方法)。这样,每个消息都会实现其句柄版本,并且您可以享受多态性的好处:
此代码将根据您的需要返回“Class:Message1”。
I am not sure what your handle(..) methods are supposed to do, but since they depend on the specific instance of Message, you should consider adding handle() method to your Message class (as abstract method). This way, each message implements its version of handle, and you can enjoy the benefits of polymorphism:
This code will return "Class: Message1", as you need.