Java实验3.doc_第1页
Java实验3.doc_第2页
Java实验3.doc_第3页
Java实验3.doc_第4页
Java实验3.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

实验3继承和多态一、 实验目的:1、 学习和使用类的继承。2、 掌握关键字super的意义和用法。3、 学习掌握类的方法覆盖技术。4、 熟悉Object类,以及它提供给子类的方法equals、toString、clone。5、 学习掌握修饰符protected和final的用法。6、 学习掌握抽象类的概念和使用方法。7、 学习掌握多态性和动态绑定的概念,学习使用多态进行程序设计。8、 学习掌握接口的概念和定义接口的方法。9、 学习使用Cloneable接口和clone方法进行对象内容的复制。10、 理解浅复制和深复制的概念,掌握覆盖clone方法进行对象内容深复制的技术。二、 实验任务:1、 使用Java SDK建立一个非图形化的标准Java程序学习和使用类的继承、掌握关键字super的意义和用法、掌握类的方法覆盖技术、熟悉Object类,以及它提供给子类的方法equals、toString、clone、学习掌握抽象类的概念和使用方法、学习掌握多态性和动态绑定的概念,学习使用多态进行程序设计。程序要求:(1) 首先创建一个类家族,其中抽象类几何图形类GeometricObject为父类,圆类Circle和矩形类Rectangle为子类。几何图形类GeometricObject中定义保护型字符串变量color,表示图形的颜色;该类要具备构造方法和两个抽象方法findArea和findPerimeter,抽象方法findArea求图形面积,抽象方法findPerimeter求图形周长。(2) Circle类和Rectangle类是GeometricObject类的子类,其中应实现父类的抽象方法。(3) 创建静态方法equalArea,用来比较图形的面积(不是以上三个类的成员方法)。方法名称如下: static boolean equalArea(GeometricObject object1, GeometricObject object2)(4) 创建静态方法displayGeometricObject,用来显示几何对象的信息(不是以上三个类的成员方法)。方法名称如下: static void displayGeometricObject(GeometricObject object)(5) 程序主方法中创建两个几何对象,一个圆和一个矩形,并用GeometricObject类的引用变量引用它们,调用equalArea比较两个对象的面积是否相等,并调用displayGeometricObject方法显示对象信息。 2、 使用Java SDK建立一个非图形化的标准Java程序,进一步学习多态特性以及接口的概念和利用接口实现多态的方法。程序要求如下:(1) 首先创建圆类Circle和圆柱体类Cylinder,其中Circle类是父类,Cylinder类是子类;(2) 创建接口Comparable,其中包含一个抽象方法compareTo,用来比较对象的大小。抽象方法compareTo的形式如下: public int compareTo(Object o);(3) 创建类ComparableCircle,该类为Circle类的子类,并实现Comparable接口。(4) 创建类ComparableCylinder,该类为Cylinder类的子类,并实现Comparable接口。(5) 创建通用类Max,其中包含通用方法max,只要月一个类实现了Comparable接口,就可以使用max方法返回两个对象中较大的一个。Max方法的方法名称为: public static Comparable max(Comparable o1, Comparable o2)(6) 程序的主方法中分别创建两个ComparableCircle类对象和两个ComparableCylinder类对象,并分别以它们为参数调用max方法,返回两个对象中面积较大的一个。3、 使用Java SDK建立一个非图形化的标准Java程序,进一步深入学习多态特性以及利用Cloneable接口和clone方法实现对象内容的拷贝,并学习消除浅拷贝(浅复制)的方法。程序要求如下:(1) 创建Circle类,表示圆;(2) 创建Name类,表示人名,其中包含三个String类型的数据成员:firstName,middlName和lastName。(3) 创建CloneableCircle类,CloneableCircle类是Circle类的子类,并实现了Cloneable接口。要求CloneableCircle类中有一个Name类型的数据成员creator,代表圆对象的创建者姓名。(4) 在CloneableCircle类中实现clone方法,以实现两个CloneableCircle类对象内容的克隆。要求实现对象内容深拷贝(深复制)。(5) 为了实现CloneableCircle类对象的深拷贝,Name类也应该实现Cloneable接口,并实现clone方法。(6) 程序的主方法中使用clone方法完成两个CloneableCircle类对象的深拷贝。三、 实验步骤:1、 使用Windows写字板编辑类GeometricObject,源程序如下: public abstract class GeometricObject protected String color; protected double weight; / Default construct protected GeometricObject() color = white; weight = 1.0; / Construct a geometric object protected GeometricObject(String color, double weight) this.color = color; this.weight = weight; / Getter method for color public String getColor() return color; / Setter method for color public void setColor(String color) this.color = color; / Getter method for weight public double getWeight() return weight; / Setter method for weight public void setWeight(double weight) this.weight = weight; / Abstract method public abstract double findArea(); / Abstract method public abstract double findPerimeter();2、 使用Windows写字板编辑抽象类GeometricObject的派生类Circle,源程序如下:public class Circle extends GeometricObject protected double radius; / Default constructor public Circle() this(1.0, white, 1.0); / Construct circle with specified radius public Circle(double radius) super(white, 1.0); this.radius = radius; / Construct a circle with specified radius, weight, and color public Circle(double radius, String color, double weight) super(color, weight); this.radius = radius; / Getter method for radius public double getRadius() return radius; / Setter method for radius public void setRadius(double radius) this.radius = radius; / Implement the findArea method defined in GeometricObject public double findArea() return radius*radius*Math.PI; / Implement the findPerimeter method defined in GeometricObject public double findPerimeter() return 2*radius*Math.PI; / Override the equals() method defined in the Object class public boolean equals(Circle circle) return this.radius = circle.getRadius(); / Override the toString() method defined in the Object class public String toString() return Circle radius = + radius; 3、 使用Windows写字板编辑抽象类GeometricObject的派生类Rectangle,源程序如下:public class Rectangle extends GeometricObject protected double width; protected double height; / Default constructor public Rectangle() this(1.0, 1.0, white, 1.0); / Construct a rectangle with specified width and height public Rectangle(double width, double height) this.width = width; this.height = height; / Construct a rectangle with specified width, height, weight, and color public Rectangle(double width, double height, String color, double weight) super(color, weight); this.width = width; this.height = height; / Getter method for width public double getWidth() return width; / Setter method for width public void setWidth(double width) this.width = width; / Getter method for height public double getHeight() return height; / Setter method for height public void setHeight(double height) this.height = height; / Implement the findArea method in GeometricObject public double findArea() return width*height; / Implement the findPerimeter method in GeometricObject public double findPerimeter() return 2*(width + height); / Override the equals() method defined in the Object class public boolean equals(Rectangle rectangle) return (width = rectangle.getWidth() & (height = rectangle.getHeight(); / Override the toString() method defined in the Object class public String toString() return Rectangle width = + width + and height = + height; 4、 使用Windows写字板编辑类TestPolymorphism,源代码如下:public class TestPolymorphism / Main method public static void main(String args) / Declare and initialize two geometric objects GeometricObject geoObject1 = new Circle(5); GeometricObject geoObject2 = new Rectangle(5, 3); System.out.println(The two objects have the same area? + equalArea(geoObject1, geoObject2); / Display circle displayGeometricObject(geoObject1); / Display rectangle displayGeometricObject(geoObject2); / A method for comparing the areas of two geometric objects static boolean equalArea(GeometricObject object1, GeometricObject object2) return object1.findArea() = object2.findArea(); / A method for displaying a geometric object static void displayGeometricObject(GeometricObject object) System.out.println(); System.out.println(object.toString(); System.out.println(The area is + object.findArea(); System.out.println(The perimeter is + object.findPerimeter(); 5、 把上面编辑的几个源程序保存成Java源程序文件(扩展名为java),程序文件名分别为GeometricObject.java、Circle.java、Rectangle.java、TestPolymorphism.java。6、 进入命令提示符状态,在源程序文件存放目录下对以上文件进行编译。观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。7、 如果编译正确,则键入如下命令行,使用Java解释器运行源程序:java TestPolymorphism程序运行结果如下:8、 使用Windows写字板编辑编辑类TestInterface源程序如下:public class TestInterface / Main method public static void main(String args) / Create two comarable circles ComparableCircle circle1 = new ComparableCircle(5); ComparableCircle circle2 = new ComparableCircle(4); / Display the max circle Comparable circle = Max.max(circle1, circle2); System.out.println(The max circles radius is + (Circle)circle).getRadius(); System.out.println(circle); / Create two comarable cylinders ComparableCylinder cylinder1 = new ComparableCylinder(5, 2); ComparableCylinder cylinder2 = new ComparableCylinder(4, 5); / Display the max cylinder Comparable cylinder = Max.max(cylinder1, cylinder2); System.out.println(); System.out.println(cylinder1s volume is + cylinder1.findVolume(); System.out.println(cylinder2s volume is + cylinder2.findVolume(); System.out.println(The max cylinders tradius is + (Cylinder)cylinder).getRadius() + ntttlength is + (Cylinder)cylinder).getLength() + ntttvolume is + (Cylinder)cylinder).findVolume(); System.out.println(cylinder); / ComparableCircle is a subclass of Circle, which implements the/ Comparable interfaceclass ComparableCircle extends Circle implements Comparable / Construct a CompareCircle with specified radius public ComparableCircle(double r) super(r); / Implement the compareTo method defined in Comparable public int compareTo(Object o) if (getRadius() (Circle)o).getRadius() return 1; else if (getRadius() (Cylinder)o).findVolume() return 1; else if (findVolume() 0) return o1; else return o2; 13、 将上面编辑的源程序保存成Java源程序文件(扩展名为java),程序文件名分别为TestInterface .java、Circle.java、Cylinder.java、Max.java。a) 进入命令提示符状态,在源程序文件存放目录下分别编译以上源程序文件。观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。 b) 如果编译正确,则键入如下命令行,使用Java解释器运行源程序:java TestInterface程序运行结果如下:14、 使用Windows写字板编辑编辑类Circle,源代码如下:public class Circle protected double radius; public Circle() this(1.0); public Circle(double radius) this.radius = radius; public double getRadius() return radius; public void setRadius(double radius) this.radius = radius; public double findArea() return radius*radius*Math.PI; public double findPerimeter() return 2*radius*Math.PI; public boolean equals(Circle circle) return this.radius = circle.getRadius(); public String toString() return Circle radius = + radius; 15、 使用Windows写字板编辑编辑类TestCloneable,源代码如下:public class TestCloneable / Main method public static void main(String args) / Declare and create an instance of CloneableCircle CloneableCircle c1 = new CloneableCircle(5); CloneableCircle c2 = (CloneableCircle)c1.clone(); System.out.println(After copying c1 to circl2); / Check if two variables point to the same object if (c1 = c2) System.out.println(c1 and c2 reference to the same object); else System.out.println(c1 and c2 dont point to the same object); / Check if two objects are of identical contents if (c1.equals(c2) System.out.println(c1 and c2 have the same contents); else System.out.println(c1 and c2 dont have the same contents); / Modify c1s radius, name c1.setRadius(10); c1.getCreator().setFirstname(Michael); c1.getCreator().setMi(Z); / Display c1 and c2 System.out.println(nAfter modifying c1); System.out.println(c1 + c1); System.out.println(c2 + c2); System.out.println(); if (c1 instanceof Cloneable) System.out.println(A CloneableCircle objec is cloneable); else System.out.println(A CloneableCircle objec is not cloneable); / Check if a Circle object is cloneable Circle c = new Circle(); if (c instanceof Cloneable) System.out.println(A Circle object is cloneable); else System.out.println(A Circle object is not cloneable); / CloneableCircle is a subclass of Circle, which implements the/ Cloneable interfaceclass CloneableCircle extends Circle implements Cloneable / Store the creator of the object private Name creator = new Name(Yong, D, Liang); / Construct a CloneableCircle with specified radius public CloneableCircle(double radius) super(radius); / Getter method for creator public Name getCreator() return creator; / Setter method for creator public void setCreator(Name

温馨提示

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

评论

0/150

提交评论