今天编程中,遇到了这么一个问题,虽然解决了,但是百思不得其解。问题的产生:
程序抛出java.lang.ClassCastException
解决方案:
把代码中的“return( new IMServiceImpl( serviceName ) );”
改为
IMServiceImpl imServiceImpl; 
imServiceImpl = new IMServiceImpl( ServiceName );
return imServiceImpl;就好了,难道对象有没有名字竟然造成了这么大的区别?

解决方案 »

  1.   

    ClassCastException
    Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException: 
         Object x = new Integer(0);
         System.out.println((String)x);
    你的错误的话,应该把全部代码帖出来,应该就知道了那里的问题了 !
      

  2.   

    暂且不管抛出的异常,也不管函数的声明,只看这两段代码1 return( new IMServiceImpl( serviceName ) );”2 
    IMServiceImpl imServiceImpl; 
    imServiceImpl = new IMServiceImpl( ServiceName );
    return imServiceImpl;难道有什么区别吗?
    或者说,这两种方式产生的对象类型竟然不一样?
    很邪门啊。
      

  3.   

    对了,由于笔误,第二段代码中的变量ServiceName应为serviceName. 
    sorry
      

  4.   

    所以才觉得奇怪啊,这个函数要求返回一个实现了
    IMService接口的对象,而IMServiceImple就是符合
    该要求的东西.嗯,如果这个对象需要序列话,通过RMI传输,有没有
    名字有区别吗?