Here's the question, and I'll answer it later.
Known classes have test and Test2, ask two times what the output of the main program (Serializeutil is just a serialized tool class)
Class Test
Public classTestImplementsserializable{Private Static Final LongSerialversionuid = 1L; Private intnum; Private transientString str; PublicTest () { This. num = 0xFFFF; This. str = "string"; } PublicTest (intnum, String str) { This. num =num; This. str =str; } @Override PublicString toString () {returnnum + "," +str; }}
Class Test2
Public classTest2Implementsexternalizable,serializable{Private Static Final LongSerialversionuid = 1L; Private intnum; Private transientString str; PublicTest2 () { This. num = 0xFFFF; This. str = "Hello"; } PublicTest2 (intnum, String str) { This. num =num; This. str =str; } @Override Public voidWriteexternal (ObjectOutput out)throwsioexception {out.write (num); } @Override Public voidReadexternal (ObjectInput in)throwsIOException, ClassNotFoundException { This. num =In.read (); } @Override PublicString toString () {returnnum + "," +str; }}
Two main programs:
Public Static void Main (string[] args) { new Test (); New File ("D:/test.bin"); Serializeutil.writeobject (file,test); = (Test) serializeutil.readobject (file); SYSTEM.OUT.PRINTLN (test); System.out.println (test2); }
Public Static void Main (string[] args) { new Test2 (); New File ("D:/test2.bin"); Serializeutil.writeobject (FILE,TEST2); = (Test2) serializeutil.readobject (file); System.out.println (test2); System.out.println (t); }
The code attached to Serializeutil can be ignored--
1 Public classSerializeutil {2 3 Public Static voidwriteobject (File file,object Object) {4FileOutputStream FileOutputStream =NULL;5 Try {6FileOutputStream =Newfileoutputstream (file);7ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream);8 Objectoutputstream.writeobject (object);9 Objectoutputstream.flush ();Ten}Catch(IOException e) { One e.printstacktrace (); A}finally { - if(fileoutputstream!=NULL){ - Try { the fileoutputstream.close (); -}Catch(IOException e) {} - } - } + } - + Public StaticObject readobject (file file) { AFileInputStream FileInputStream =NULL; at Try { -FileInputStream =Newfileinputstream (file); -ObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); - returnObjectinputstream.readobject (); -}Catch(classnotfoundexception|IOException E) { - e.printstacktrace (); in}finally { - if(fileinputstream!=NULL){ to Try { + fileinputstream.close (); -}Catch(IOException e) {} the } * } $ return NULL;Panax Notoginseng } - the}Serialization Tool Class
The answers are as follows:
/* First Main program: 65535,string65535,null the second main program: 65535,hello255,hello */
Answer
A question about Java serialization, see how much you know ————