欢迎来到天天文库
浏览记录
ID:37923027
大小:52.00 KB
页数:7页
时间:2019-06-02
《JAVA 继承和多态实验报告》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验报告实验项目名称:继承和多态(所属课程:Java语言程序设计)院系:专业班级:姓名:学号:实验地点:指导老师:本实验项目成绩:教师签字:日期:1.实验目的(1)掌握类的继承机制。(2)熟悉类中成员变量和方法的访问控制。(3)熟悉方法或构造方法多态性。2.实验内容(1)模拟编写程序,理解类的继承、多态、继承和多态规则。(2)独立编程,实现类的继承和多态。3.实验作业设计一个类Shape(图形)包含求面积和周长的area()方法和perimeter()方法以及设置颜色的方法SetColor(),并利用Java多态技术设计其子类Cir
2、cle(圆形)类、Rectangle(矩形)类和Triangle(三角形)类,并分别实现相应的求面积和求周长的方法。每个类都要覆盖toString方法。海伦公式:三角形的面积等于s(s-a)(s-b)(s-c)的开方,其中s=(a+b+c)/2程序代码为:Class包packageClass;publicclassShape{privateStringcolor="while";publicShape(Stringcolor){this.color=color;}publicvoidsetColor(Stringcolor){thi
3、s.color=color;}publicStringgetColor(){7实验报告returncolor;}publicdoublegetArea(){return0;}publicdoublegetPerimeter(){return0;}publicStringtoString(){return"color:"+color;}}packageClass;publicclassCircleextendsShape{privatedoubleradius;publicCircle(Stringcolor,doubleradius
4、){super(color);this.radius=radius;}publicvoidsetRadius(doubleradius){this.radius=radius;}publicdoublegetRadius(){returnradius;}publicdoublegetCircleArea(){return3.14*radius*radius;}publicdoublegetCirclePerimeter(){return3.14*2*radius;}7实验报告publicStringtoString(){return
5、"TheAreais:"+getCircleArea()+"ThePerimeteris:"+getCirclePerimeter();}}packageClass;publicclassRectangleextendsShape{privatedoublewidth;privatedoubleheight;publicRectangle(Stringcolor,doublewidth,doubleheight){super(color);this.width=width;this.height=height;}publicvo
6、idsetWidth(doublewidth){this.width=width;}publicdoublegetWidth(){returnwidth;}publicvoidsetHeight(doubleheight){this.height=height;}publicdoublegetHeight(){returnheight;}publicdoublegetRectangleArea(){returnwidth*height;}publicdoublegetRectanglePerimeter(){return2*(wid
7、th+height);}7实验报告publicStringtoString(){return"TheAreais:"+getRectangleArea()+"ThePerimeteris:"+getRectanglePerimeter();}}packageClass;publicclassTriangleextendsShape{privatedoublea;privatedoubleb;privatedoublec;privatedoubles;publicTriangle(Stringcolor,doublea,doubl
8、eb,doublec,doubles){super(color);this.a=a;this.b=b;this.c=c;this.s=s;}publicvoidsetA(doublea){this.a=a;}publicdoubleg
此文档下载收益归作者所有