Java宝藏实验资源库(8)多态、抽象类和接口

一、实验目的

  1. 理解面向对象程序的基本概念。
  2. 掌握类的继承和多态的实现机制。
  3. 熟悉抽象类和接口的用法。

 二、实验内容、过程及结果 

**1.Using the classes defined in Listing 13.1, 13.2, 13.3, write a test program that creates an array of some Circle and Rectangle instances, compare the instances on the basis of area, find the largest instance and display it.

题目1:使用清单13.1、13.2和13.3中定义的类,编写一个测试程序,该程序创建一些Circle和Rectangle实例的数组,根据面积比较实例,找到最大的实例并显示它。

运行代码如下 : 

// GeometricObject.java
public abstract class GeometricObject {
    public abstract double getArea();

    public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
        if (o1.getArea() > o2.getArea()) {
            return o1;
        } else {
            return o2;
        }
    }
}
// Circle.java
public class Circle extends GeometricObject {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}
// Rectangle.java
public class Rectangle extends GeometricObject {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }
}
// TestGeometricObject.java
public class TestGeometricObject {
    public static void main(String[] args) {
        GeometricObject[] objects = new GeometricObject[4];
        objects[0] = new Circle(5);
        objects[1] = new Rectangle(3, 4);
        objects[2] = new Circle(10);
        objects[3] = new Rectangle(5, 6);

        GeometricObject maxObject = objects[0];

        for (int i = 1; i < objects.length; i++) {
            maxObject = GeometricObject.max(maxObject, objects[i]);
        }

        if (maxObject instanceof Circle) {
            System.out.println("最大的圆的面积是: " + maxObject.getArea());
        } else if (maxObject instanceof Rectangle) {
            System.out.println("最大的矩形的面积是: " + maxObject.getArea());
        }
    }
}

运行结果    

**2.Define a class named ComparableGeometricObject that extends GeometricObject and implements Comparable. Rewrite the Circle class and Rectangle class in Listing 13.2 and Listing 13.3 to extend ComparableGeometricObject. Draw the UML diagram and implement these classes. Write a test program that creates an array of some Circle and Rectangle instances, sort the array and display the sorted elements.

**2.定义一个名为ComparableGeometricObject的类,它扩展了GeometricObject并实现了Comparable。重写清单13.2和清单13.3中的Circle类和Rectangle类,以扩展ComparableGeometricObject。绘制UML图并实现这些类。编写一个测试程序,创建一个由一些Circle和Rectangle实例组成的数组,对数组进行排序并显示排序后的元素。以上翻译结果来自有道神经网络翻译(YNMT)· 通用场景

运行代码如下 : 

// GeometricObject.java
public abstract class GeometricObject {
    public GeometricObject() {
    }

    public abstract double getArea();

    public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
        if (o1.getArea() > o2.getArea()) {
            return o1;
        } else {
            return o2;
        }
    }
}
// ComparableGeometricObject.java
public abstract class ComparableGeometricObject extends GeometricObject implements Comparable<GeometricObject> {
    public ComparableGeometricObject() {
    }

    @Override
    public int compareTo(GeometricObject obj) {
        if (this.getArea() > obj.getArea()) {
            return 1;
        } else if (this.getArea() < obj.getArea()) {
            return -1;
        } else {
            return 0;
        }
    }
}
// Circle.java
public class Circle extends ComparableGeometricObject {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}
// Rectangle.java
public class Rectangle extends ComparableGeometricObject {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }
}
// TestComparableGeometricObject.java
import java.util.Arrays;

public class TestComparableGeometricObject {
    public static void main(String[] args) {
        ComparableGeometricObject[] objects = new ComparableGeometricObject[4];
        objects[0] = new Circle(5);
        objects[1] = new Rectangle(3, 4);
        objects[2] = new Circle(10);
        objects[3] = new Rectangle(5, 6);

        Arrays.sort(objects);

        System.out.println("排序后的对象:");
        for (ComparableGeometricObject obj : objects) {
            if (obj instanceof Circle) {
                System.out.println("圆的面积: " + obj.getArea());
            } else if (obj instanceof Rectangle) {
                System.out.println("矩形的面积: " + obj.getArea());
            }
        }
    }
}

运行结果  

三、实验结论

       通过本次实验实践了类的继承和多态的实现机制的知识和操作,熟悉了抽象类和接口的用法得到了理论要与实践相结合的感悟,在实践操作方面可以再努努力!

 结语    

你都不知道山有多高

你怎么知道终点在哪

!!!

  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值