《Java实验3-.pdf》由会员分享,可在线阅读,更多相关《Java实验3-.pdf(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验 3 继承和多态一、实验目的:1、学习和使用类的继承。2、掌握关键字 super的意义和用法。3、学习掌握类的方法覆盖技术。4、熟悉 Object类,以及它提供给子类的方法equals、toString、clone。5、学习掌握修饰符protected和 final 的用法。6、学习掌握抽象类的概念和使用方法。7、学习掌握多态性和动态绑定的概念,学习使用多态进行程序设计。8、学习掌握接口的概念和定义接口的方法。9、学习使用 Cloneable接口和 clone方法进行对象内容的复制。10、理解浅复制和深复制的概念,掌握覆盖 clone 方法进行对象内容深复制的技术。二、实验任务:1、使用
2、Java SDK建立一个非图形化的标准Java程序学习和使用类的继承、掌握关键字super 的意义和用法、掌握类的方法覆盖技术、熟悉Object类,以及它提供给子类的方法equals、toString、clone、学习掌握抽象类的概念和使用方法、学习掌握多态性和动态绑定的概念,学习使用多态进行程序设计。程序要求:(1)首先创建一个类家族,其中抽象类几何图形类GeometricObject为父类,圆类Circle 和矩形类Rectangle 为子类。几何图形类GeometricObject中定义保护型字符串变量color,表示图形的颜色;该类要具备构造方法和两个抽象方法findArea 和 fi
3、ndPerimeter,抽象方法 findArea 求图形面积,抽象方法 findPerimeter求图形周长。(2)Circle 类和 Rectangle类是 GeometricObject类的子类,其中应实现父类的抽象方法。(3)创建静态方法 equalArea,用来比较图形的面积(不是以上三个类的成员方法)。方法名称如下:static boolean equalArea(GeometricObject object1,GeometricObject object2)(4)创建静态方法displayGeometricObject,用来显示几何对象的信息(不是以上三个类的成员方法)。方法名称
4、如下:static void displayGeometricObject(GeometricObject object)(5)程序主方法中创建两个几何对象,一个圆和一个矩形,并用GeometricObject类的引用变量引用它们,调用 equalArea比较两个对象的面积是否相等,并调用displayGeometricObject方法显示对象信息。2、使用 Java SDK建立一个非图形化的标准Java程序,进一步学习多态特性以及接口的概念和利用接口实现多态的方法。程序要求如下:(1)首先创建圆类 Circle 和圆柱体类 Cylinder,其中 Circle 类是父类,Cylinder 类
5、是子类;(2)创建接口Comparable,其中包含一个抽象方法compareTo,用来比较对象的大小。抽象方法compareTo的形式如下:public int compareTo(Object o);(3)创 建 类 ComparableCircle,该类 为 Circle 类 的 子类,并 实 现Comparable接口。(4)创建类 ComparableCylinder,该类为Cylinder 类的子类,并实现Comparable接口。(5)创建通用类Max,其中包含通用方法max,只要月一个类实现了Comparable接口,就可以使用max 方法返回两个对象中较大的一个。Max 方法
6、的方法名称为:public static Comparable max(Comparable o1,Comparable o2)(6)程序的主方法中分别创建两个ComparableCircle 类对象和两个ComparableCylinder类对象,并分别以它们为参数调用max 方法,返回两个对象中面积较大的一个。3、使用 Java SDK建立一个非图形化的标准Java程序,进一步深入学习多态特性以及利用 Cloneable接口和 clone 方法实现对象内容的拷贝,并学习消除浅拷贝(浅复制)的方法。程序要求如下:(1)创建 Circle 类,表示圆;(2)创建 Name类,表示人名,其中包含
7、三个String 类型的数据成员:firstName,middlName 和 lastName。(3)创建 CloneableCircle类,CloneableCircle类是 Circle 类的子类,并实现了 Cloneable接口。要求 CloneableCircle 类中有一个 Name类型的数据成员 creator,代表圆对象的创建者姓名。(4)在 CloneableCircle类中实现 clone方法,以实现两个 CloneableCircle类对象内容的克隆。要求实现对象内容深拷贝(深复制)。(5)为了实现CloneableCircle 类对象的深拷贝,Name 类也应该实现Clo
8、neable接口,并实现 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
9、 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
10、 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 rad
11、ius;/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);th
12、is.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 findPerimet
13、er 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 toStri
14、ng()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 heigh
15、t 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 widt
16、h 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 Geome
17、tricObject 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()&(hei
18、ght=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 initial
19、ize 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(geoObj
20、ect2);/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.prin
21、tln();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、进入命令提示符状态,在源程序文件存放目录下对以上
22、文件进行编译。观察编译后源程序文件存放目录中的文件,熟悉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);C
23、omparableCircle 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(
24、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.pri
25、ntln(The max cylinders tradius is +(Cylinder)cylinder).getRadius()+ntttlength is +(Cylinder)cylinder).getLength()+ntttvolume is +(Cylinder)cylinder).findV olume();System.out.println(cylinder);/ComparableCircle is a subclass of Circle,which implements the/Comparable interface class ComparableCircle e
26、xtends 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()
27、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 写字
28、板编辑编辑类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 find
29、Perimeter()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
30、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.
31、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(M
32、ichael);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 i
33、s 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 interface class C
34、loneableCircle 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;/Set
35、ter method for creator public void setCreator(Name name)creator=name;/Override the protected clone method defined in the Object class public Object clone()Object o=null;try o=(CloneableCircle)super.clone();catch(CloneNotSupportedException ex)return null;/(CloneableCircle)o).creator=(Name)creator.clo
36、ne();return o;/Override the toString method defined in the Object class public String toString()return super.toString()+creator.getFullname();class Name implements Cloneable private String firstName;private String middlName;private String lastName;public Name(String f,String m,String l)firstName=f;m
37、iddlName=m;lastName=l;public String getFullname()String s=firstName+middlName+lastName;return s;public void setFirstname(String s)firstName=s;public void setMi(String s)middlName=s;public void setLastName(String s)lastName=s;public Object clone()Object o=null;try o=(Name)super.clone();catch(CloneNotSupportedException ex)return null;return o;16、16、将上面编辑的源程序保存成Java源程序文件(扩展名为 java),程序文件名分别为、Circle.java、TestCloneable.java。a)进入命令提示符状态,在源程序文件存放目录下分别编译以上源程序文件。观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。b)如果编译正确,则键入如下命令行,使用Java解释器运行源程序:java TestCloneable 程序运行结果如下:四、实验难点:多态性的实现和对象内容的复制是本次实验的难点,同学们要重点理解。
限制150内