欢迎来到天天文库
浏览记录
ID:61513023
大小:61.50 KB
页数:5页
时间:2021-02-09
《Java实验报告样本.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、信息科学与工程学院《Java程序设计》上机实验报告专业班级电技电信0901姓名刘雄楚学号0实验时间2011年9月30日指导教师程骅成绩实验名称面向对象程序设计的继承、多态等特性的练习实验目的1)掌握类的继承机制。2)熟悉类中成员变量和方法的访问控制。3)熟悉方法或构造方法多态性。4)熟悉接口的定义方法。1classPerson{publicStringname;publiccharsex;publicintage;Person(Stringname,charsex,intage){this.name=name;this.sex=sex;this.age
2、=age;}}classStudentextendsPerson{publicStringclassno;publicStringsno;publicvoidupdateAge(intage){this.age=age;}publicStringtoString(){System.out.println(this.name+""+this.sex+""+this.age+""+this.classno+""+this.sno);returnthis.name+""+this.sex+""+this.age+""+this.classno+""+this
3、.sno;}Student(Stringname,charsex,intage,Stringclassno,Stringsno){super(name,sex,age);this.classno=classno;this.sno=sno;}}publicclassTestStudentPerson{publicstaticvoidmain(Stringargs[]){Students1=newStudent("jeck",'n',20,"","2");s1.updateAge(21);s1.toString();}}2publicclassTestVi
4、rtualInvoke{staticvoiddoStuff(Shapes){s.draw();}publicstaticvoidmain(String[]args){Circlec=newCircle();Trianglet=newTriangle();Linel=newLine();doStuff(c);doStuff(t);doStuff(l);}}classShape{voiddraw(){System.out.println("ShapeDrawing");}}classCircleextendsShape{voiddraw(){System.
5、out.println("ShapeCircle");}}classTriangleextendsShape{voiddraw(){System.out.println("ShapeTriangle");}}classLineextendsShape{voiddraw(){System.out.println("ShapeLine");}}使用抽象类abstractabstractclassShape{abstractvoiddraw();}classCircleextendsShape{voiddraw(){System.out.println("S
6、hapeCircle");}}classTriangleextendsShape{voiddraw(){System.out.println("ShapeTriangle");}}classLineextendsShape{voiddraw(){System.out.println("ShapeLine");}}使用interface接口方法interfaceShape{publicvoiddraw();}classCircleimplementsShape{publicvoiddraw(){System.out.println("ShapeCircl
7、e");}}classTriangleimplementsShape{publicvoiddraw(){System.out.println("ShapeTriangle");}}classLineimplementsShape{publicvoiddraw(){System.out.println("ShapeLine");}}思考题:importjava.util.*;abstractclassEmployee{Stringname;longno;staticlongid=1000;abstractdoublegetPay();Employee(S
8、tringname){this.name=name;id=id+1;}};classManag
此文档下载收益归作者所有