资源描述:
《<<面向对象程序设计>>实验指导》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、VV高级语言程序设计>>实验指导实验五异常处理与I/O操作(对象的串行化)一实验目的:1掌握java语言与其它语言的不同之处:异常处理与I/O操作(对象的串行化)二实验内容:1请对在实验四屮第1步骤生成的Student类进行改写,使其能被串行化,并编写测试类WriteObject,ReadObject,检测java对象的串行化和异常处理功能。importjava.io.*;importjava.util.*;classStudentimplementsSerializable{privateStringname;privateStringno;privateintage;voidsetN
2、ame(Stringname){this,name二name;}voidsetNo(Stringno){this.no=no;}voidsetAge(intage){this.age=age;}publicStudent(Stringname,Stringnojntage){this.name=name;this.no=no;this.age=age;}StringgetName(){returnname;)StringgetNo(){returnno;}intgetAge(){returnage;}publicStudent(){}publicStringtoStringO{retur
3、nname+nisH+no+Handisf,+age+n-yearold.";}}publicclassWriteObject{publicstaticvoidmain(Stringf]args){try{Student[]student=newStudent⑶;student[O]=newStudent(,,maryM,”100561O5'21);student[l]=newStudent(nlilyn,”10056106”,22);student[2]=newStudent(,rnanau,T0056107”,23);ObjectOutputStreamout=newObjectO
4、utputStream(newFileOutputStream(HstudentdatH));out.writeObject(student);out.close();}catch(Exceptione){System.out.println(,,Error:,,+e);System.exit(l);importjava.io.*;importjava.util.*;classStudentimplementsSerializable{privateStringname;privateStringno;privateintage;voidsetName(Stringname){this.
5、name=name;}voidsetNO(Stringno){this.no=no;}voidsetAge(intage){this.age=age;}publicStudent(Stringname,Stringno.intage){this.name=name;this.no=no;this.age=age;}StringgetName(){returnname;}StringgetNo(){returnno;}intgetAge(){returnage;}publicStudent(){}publicStringtoStringO{returnname+nisH+no+Mandis
6、H+age+M-yearold.n;publicclassReadObjectpublicstaticvoidmain(String[]args){try{Student[]student=newStudent[3J;student[O]=newStudent("mary","10056105°,21);student[l]=newSludent(”lily”,T0056106",22);student[2]=newStudent("nana'r,"10056107",23);ObjectOutputStreamout=newObjectOutputStream(newFileOutpu
7、tStream(ustudent.datH));out.writeObject(student);out.closeQ;ObjectlnputStreamin=newObjectInputStream(newFileInputStream(nstudent.datH));StudentlJnewStudent=(Student[])in.readObject();inti;for(i=0;i