抽象类和接口_第1页
抽象类和接口_第2页
抽象类和接口_第3页
抽象类和接口_第4页
抽象类和接口_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上南昌大学实验报告学生姓名: 王维 学 号: 专业班级: 网工121班 实验类型: 验证 综合 设计 创新 实验日期: 实验成绩: 实验5 抽象类和接口一、实验目的1、掌握抽象类abstract的概念。2、掌握接口interface的概念。 3、掌握抽象类、接口的定义、实现与使用; 4、理解接口和继承的混合使用二、实验内容 1、 课本p399 14.3题 和14.7题2、 将课本中的GeometricObject类改为接口,重新设计circle、rectangle类,实现相应的功能。三、实验要求1、通过上述程序的设计,清楚java中抽象类、接口的定义、实现与使

2、用;  四、实验环境1、PC微机;2、DOS操作系统或 Windows 操作系统;3、Sdk程序集成环境或Eclipse程序集成环境。 五、实验步骤1、输入源程序;2、利用相关工具(Eclipse运行本程序);3、查看实验结果,通过分析结果来掌握继承和多态的特征。 六、测试数据 略七、源代码14.3题:Square类:public class Square extends GeometricObject implements Colorable private double length;public Square(double length)super();th

3、is.length=length;public Square(double length,String color)super(color,true);this.length=length;public void howToColor() / TODO Auto-generated method stub System.out.println("Color all four sides");public void xx()Overridepublic double getArea() / TODO Auto-generated method stubreturn lengt

4、h*length;Overridepublic double getPerimeter() / TODO Auto-generated method stubreturn 4*length;Square测试类:public class TestSquare public static void main(String args) / TODO Auto-generated method stubGeometricObject objects = new GeometricObject5;objects0 = new Square(1);objects1 = new Square(1.5, &q

5、uot;red");objects2 = new Square(2.5);objects3 = new Square(3.0, "black");objects4 = new Square(5);for (int i = 0; i < 5; i+) System.out.println("第" + i + "对象:");if (objectsi.isFilled() (Colorable) objectsi).howToColor(); elseSystem.out.println("不可着色");

6、System.out.println("面积为:" + objectsi.getArea();System.out.println("周长为:" + objectsi.getPerimeter();14.7题Octagon类public class Octagon extends GeometricObject implements Comparable<Octagon>,Cloneable private double length;public Octagon(double length) this.length=length;Overr

7、idepublic double getArea() return (2+4/Math.sqrt(2)*length*length;Overridepublic double getPerimeter() return 0;public double getLength() return length;public void setLength(double length) this.length = length;public int compareTo(Octagon o) if(this.length>o.getLength()return 1;else if(this.lengt

8、h<o.getLength()return -1;elsereturn 1;Overrideprotected Object clone() throws CloneNotSupportedException return super.clone();Octagon测试类:public class TestOctagon public static void main(String args) / TODO Auto-generated method stubOctagon o = new Octagon(5);System.out.println("面积为:"+o.

9、getArea();System.out.println("周长为:"+o.getPerimeter();接口GeometricObjectimport java.util.Date;public interface GeometricObject public String getColor();public void setColor(String color);public boolean isFilled();public void setFilled(boolean filled);public Date getDateCreated();public Strin

10、g toString();public abstract double getArea();public abstract double getPerimeter();新的Circle类:import java.util.Date;public class Circle implements GeometricObject private String color = "white"private boolean filled;private java.util.Date dateCreated;private double radius;public Circle() d

11、ateCreated = new java.util.Date();public Circle(double radius) public String getColor() / TODO Auto-generated method stubreturn color;public void setColor(String color) / TODO Auto-generated method stubthis.color = color;public boolean isFilled() / TODO Auto-generated method stubreturn filled;public

12、 void setFilled(boolean filled) / TODO Auto-generated method stubthis.filled = filled;public Date getDateCreated() / TODO Auto-generated method stubreturn dateCreated;public double getArea() / TODO Auto-generated method stubreturn radius * radius * Math.PI;public double getPerimeter() / TODO Auto-ge

13、nerated method stubreturn 2 * radius * Math.PI;public double getRadius() return radius;public void setRadius(double radius) this.radius = radius;新的Rectangle类:import java.util.Date;public class Rectangle implements GeometricObject private String color = "white"private boolean filled;private

14、 java.util.Date dateCreated;private double width;private double height;public Rectangle() dateCreated = new java.util.Date();public Rectangle(double width, double height) this.width = width;this.height = height;public String getColor() / TODO Auto-generated method stubreturn color;public void setCol

15、or(String color) / TODO Auto-generated method stubthis.color = color;public boolean isFilled() / TODO Auto-generated method stubreturn filled;public void setFilled(boolean filled) / TODO Auto-generated method stubthis.filled = filled;public Date getDateCreated() / TODO Auto-generated method stubretu

16、rn dateCreated;public double getArea() / TODO Auto-generated method stubreturn width * height;public double getPerimeter() / TODO Auto-generated method stubreturn 2 * (width + height);public double getWidth() return width;public void setWidth(double width) this.width = width;public double getHeight()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论