您好,欢迎访问三七文档
《面向对象程序设计(JAVA)》实验报告学号:2013014744姓名:周以巍实验名称:抽象类与接口1.实验内容(标题宋体四号)实验35编写一个JAVA程序,在程序中定义一个抽象类shape,再定义两个shape类的子类rectangle、circle类,在子类中实现父类的抽象方法。实验41编写一个JAVA程序,在程序中定义一个接口shape,定义一个类cylinder实现接口shape,在cylinder类中实现shape接口中的抽象方法。实验42编写一个JAVA程序,在程序中定义一个接口achievement,定义一个父类person,定义一个子类student继承person类并实现achievement接口,在子类student中实现接口中的抽象方法并调用父类的方法。2.代码与结果:实验35:abstractclassShape{abstractfloatArea();abstractvoidprintArea();}classRectangleextendsShape{intwidth;intlength;publicRectangle(intnewWidth,intnewLength){width=newWidth;length=newLength;}floatArea(){returnwidth*length;}voidprintArea(){System.out.println(矩形面积:+width*length);}}classCircleextendsShape{finalfloatpi=3.14F;intradius;publicCircle(intnewRadius){radius=newRadius;}floatArea(){returnpi*radius*radius;}voidprintArea(){System.out.println(园的面积:+pi*radius*radius);}}classchouxiang{publicstaticvoidmain(String[]args){Rectangles1=newRectangle(3,4);Circles2=newCircle(2);s1.printArea();s2.printArea();}}结果:实验41:代码:importjava.text.DecimalFormat;interfaceShape{finaldoublepi=3.14;doublearea();doublevolume();}classCylinderimplementsShape{privatedoubleradius;privateintheight;publicCylinder(doubler,inth){radius=r;height=h;}publicdoublearea(){returnpi*radius*radius;}publicdoublevolume(){returnarea()*height;}}classMyinterface{publicstaticvoidmain(String[]args){Cylindera=newCylinder(2,3);DecimalFormatmyFormat=newDecimalFormat(0.00);System.out.println(圆柱体面积+myFormat.format(a.area()));System.out.println(圆柱体体积+myFormat.format(a.volume()));}}结果:、实验42代码:interfaceAchievement{floataverage();}classPerson{Stringname;intage;publicPerson(StringnewName,intnewAge){name=newName;age=newAge;}publicvoidintroduce(){System.out.println(你好我是+name+,今年+age+岁);}}classStudentextendsPersonimplementsAchievement{intChinese;intMath;intEnglish;publicStudent(StringnewName,intnewAge){super(newName,newAge);}publicvoidsetScore(intc,intm,inte){Chinese=c;Math=m;English=e;}publicfloataverage(){return(Chinese+Math+English)/3;}}classJieKou{publicstaticvoidmain(String[]args){Students1=newStudent(张三,16);s1.introduce();s1.setScore(80,90,80);System.out.println(平均分:+s1.average());}}结果:
本文标题:作业
链接地址:https://www.777doc.com/doc-5645578 .html