Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我想知道如何在ServerSocket程序的服务器端检查字符串是否是Class的实例。客户端提供了一个类Myclass1的对象,服务器必须检查从客户端读取的字符串是否是MyClass1或另一个类的实例(例如MyClass2,String,Integer等)。

那是我的代码:

  ObjectInputStream object_input = new ObjectInputStream(sock.getInputStream());
  String string = object_input.readLine();
  if (string instanceof MyClass1){...}
  else if(string instanceof MyClass2){...}


这会导致错误,我不知道如何解决。请帮我

最佳答案

重写您的代码,如下所示:

ObjectInputStream objectInput = new ObjectInputStream(sock.getInputStream());

        Object objectFromClient = objectInput.readObject();

        if (objectFromClient instanceof MyClass1) {

        } else if (objectFromClient instanceof MyClass2) {

        }// etc..

10-08 19:42