实验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 = \ 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, \ }
// Construct circle with specified radius public Circle(double radius) {
super(\ 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 \ } }
3、使用Windows写字板编辑抽象类GeometricObject的派生类Rectangle,源
程序如下:
public class Rectangle extends GeometricObject {
protected double width; protected double height;
// Default constructor public Rectangle() {
this(1.0, 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; }