




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五 继承与多态专业班级学 号姓 名实验学时2实验类型验证性实验地点软件工程实验室实验时间2013-11-21指导老师曹记东实验成绩 年 月 日 一、实验目的:理解继承与多态的概念,掌握三种访问控制的区别,掌握抽象类、接口与包的应用。二、实验内容:1. 参考教材P112,示例程序C5_17,掌握抽象类的应用。2. 参考教材P116,示例程序C5_19,掌握接口的使用方法。3. 参考教材P121,示例程序C5_20,掌握包的使用方法。三、实验要求:1.完成示例程序的验证。2.写出实验报告。四、实验步骤与结果:(由学生按照实验内容完成)1. 示例程序c5_17import 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 c5_18 extends Applet/* * */private static final long serialVersionUID = -5589238944558587862L;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. 示例程序C5_19import 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 c5_20 extends Applet /* * */private static final long serialVersionUID = 2664639751369334604L;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 Are
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 乐理音程考试题库及答案
- 森林防护巡护知识培训课件
- 森林家庭防火知识培训课件
- 棋王课件中职
- 海南省养老护理员职业资格技师考试题(含答案)
- 2025年精细化工作业面试宝典涵盖各类工艺作业预测题
- 2025年裁判台球考试题及答案
- 2025年公路水运安全员(交安abc)考试考核题库答案
- 2025年项目经理从业资格认证考试模拟试题集及答题技巧
- 2025年软件工程师职称评定预测试题及答案参考
- 液化天然气码头设计规范JTS 165-5-2021
- TFSI 098-2022 《氨基硅油》发布稿
- 2025年高考物理二轮复习:11个模块知识讲义
- 胖东来面试题及答案
- 2024初级注册安全工程师笔试真题含答案
- 海宁市食品农产品检验检测中心改建项目环评报告
- 石油化工设计能耗计算标准
- 泵站运行管理规范
- 土豆采购合同协议
- 幼儿托育服务与管理
- 2024版机电质量标准化管理图册
评论
0/150
提交评论