




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五 继承与多态专业班级学 号姓 名实验学时2实验类型验证性实验地点软件工程实验室实验时间指导老师曹记东实验成绩 年 月 日一、实验目的:理解继承与多态的概念,掌握三种访问控制的区别,掌握抽象类、接口与包的应用。二、实验内容:1. 参考教材P112,示例程序C5_17,掌握抽象类的应用。2. 参考教材P116,示例程序C5_19,掌握接口的使用方法。3. 参考教材P121,示例程序C5_20,掌握包的使用方法。三、实验要求:1.完成示例程序的验证。2.写出实验报告。四、实验步骤与结果:(由学生按照实验内容完成)程序如下程序1.package d; import java.awt.*;import java.applet.*;abstract class Shapes /定义一个抽象类Shapes public int x,y; /x、y为画图的坐标 public int width,height; public Shapes(int x,int y,int width,int height) this.x=x; this.y=y; this.width=width; this.height=height; abstract double getArea();/求图形面积的抽象方法 abstract double getPerimeter();/求图形周长的抽象方法class Square extends Shapes /由抽象类Shapes派生的子类矩形类 public double getArea()return(width*height); public double getPerimeter()return(2*width+2*height); public Square(int x,int y,int width,int height) super(x,y,width,height); class Triangle extends Shapes /由抽象类Shapes派生的子类三角形类 public double c;/斜边 public double getArea()return(0.5*width*height); public double getPerimeter()return(width+height+c); public Triangle(int x,int y,int base,int height) super(x,y,base,height); c=Math.sqrt(width*width+height*height); class Circle extends Shapes /由抽象类Shapes派生的子类圆类 public double r;/半径 public double getArea()return(r*r*Math.PI); public double getPerimeter()return(2*Math.PI*r); public Circle(int x,int y,int width,int height) super(x,y,width,height); r=(double)width/2.0; public class Q extends Applet Square Box=new Square(5,15,25,25); Triangle tri=new Triangle(5,50,8,4); Circle Oval=new Circle(5,90,25,25); public void paint(Graphics g) /画正方形 g.drawRect(Box.x,Box.y,Box.width,Box.height); g.drawString(Box Area:+Box.getArea(),50,35); g.drawString(Box Perimeter:+Box.getPerimeter(),50,55); g.drawString(tri Area:+tri.getArea(),50,75); g.drawString(tri Perimeter:+tri.getPerimeter(),50,95); /画圆 g.drawOval(Oval.x,Oval.y,Oval.width,Oval.height); g.drawString(oval Area:+Oval.getArea(),50,115); 运行结果 程序2.package cc;import java.awt.*;import java.applet.*;interface Shapes abstract double getArea(); abstract double getPerimeter();class Coordinates int x,y; public Coordinates(int x,int y) this.x=x; this.y=y; /Square Coordinates Shapesclass Square extends Coordinates implements Shapes public int width,height; public double getArea()return(width*height); public double getPerimeter()return(2*width+2*height); public Square(int x,int y,int width,int height) super(x,y); this.width=width; this.height=height; class Triangle extends Coordinates implements Shapes public int width,height; public double c; public double getArea()return(0.5*width*height); public double getPerimeter()return(width+height+c); public Triangle(int x,int y,int base,int height) super(x,y); width=base; this.height=height; c=Math.sqrt(width*width+height*height); class Circle extends Coordinates implements Shapes public int width,height; public double r; public double getArea()return(r*r*Math.PI); public double getPerimeter()return(2*Math.PI*r); public Circle(int x,int y,int width,int height) super(x,y); this.width=width; this.height=height; r=(double)width/2.0; public class D extends Applet Square Box=new Square(5,15,25,25); Triangle tri=new Triangle(5,50,8,4); Circle Oval=new Circle(5,90,25,25); public void paint(Graphics g) g.drawRect(Box.x,Box.y,Box.width,Box.height); g.drawString(Box Area:+Box.getArea(),50,35); g.drawString(Box Perimeter:+Box.getPerimeter(),50,55); g.drawString(tri Area:+tri.getArea(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 污水治理合同(标准版)
- 旅社出兑合同(标准版)
- 四川达州萼山职业技术学校教师招聘考试真题2024
- 高速公路视频监控系统设计方案及发展方向
- 2025年建筑施工企业安管人员考试(专职安全生产管理人员C1类)冲刺模拟试题及答案
- 服务方案工作计划安排及进度保证措施
- 2024年省燃气经营企业从业人员考试(压缩天然气场站工)仿真试题及答案四
- 考点解析-人教版八年级上册物理《物态变化》专项训练练习题(含答案详解)
- 2025年燃气经营企业从业人员考试综合能力测试题及答案
- 2025数控中级考试试题及答案
- 2025年河北省石家庄市公安辅警招聘知识考试题(含答案)
- 2025年介入心脏病学临床技能考核答案及解析
- 苏联入侵阿富汗
- 2025广东清远市公安局第二次选调事业编制人员18人笔试备考试题及答案解析
- 2025广州市职工劳动合同书范本
- 2025年江苏公务员考试试题真题
- 企业天然气调压安全培训课件
- 2025-2030太极拳教学市场细分领域及渠道布局与竞争战略分析报告
- 全国2025年10月自考03709《马克思主义基本原理概论》真题及答案
- DB32-T 5160-2025 传媒行业数据分类分级指南
- 2 中国人首次进入自己的空间站 公开课一等奖创新教案 统编版语文八年级上册
评论
0/150
提交评论