如何找出来自对象输入流的对象类?

时间:2016-05-28 02:19:49

标签: java

我正在做一个java项目,我需要检查输入流中的给定对象是否属于" xyzsomething。"

我知道我可以使用instanceof,但是当我检查instanceof到类名" xyzsomething,"它给了我一个编译错误,因为它没有识别类名。我该怎么办?

编辑:对不起,我是新来的。这是代码和错误:

 if ((input instanceof ShotCoordinatesServer))  <--- error occurs on this
                                                it won't recognize class.
 {
      checkHit((ShotCoordinatesServer)input); <--- error occurs on this too
 }

ERROR: cannot find symbol

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

Tables 

Events

Id_event
description
etc
id_user

event_posts
id_event
id_post
id_user
description

这应该编译...只要符号 ObjectInputStream ois = ... Object something = ois.readObject(); if (something instanceof MyClass) { ((MyClass) something).someMethodOfMyClass(); } 在编译时已知。

在您的示例中,似乎在编译时不知道MyClass。修复可能只是ShotCoordinatesServer课程。或者您可能需要将其添加到编译时类路径中。

如果在编译时类完全不可用,那么就不能使用import和类型转换。相反,您将需要使用反射...或者可能测试并转换为超出instanceof的超类型的接口类型。

但是如果你只是想找出你刚读过的班级的名字......

ShotCoordinatesServer
相关问题