面向对象程序设计(双语)|| 实验四:多态、抽象类及接口(Java版)

发布于:2025-04-20 ⋅ 阅读:(22) ⋅ 点赞:(0)

一、实验目的

  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.

使用在列表清单13.1、13.2、13.3定义的类,写一个测试程序,创建一些Circle和Retangle的实例,以面积为标准去比较这些实例,找出面积最大的实例并显示它。

程序设计思路

import java.util.Date;

public abstract class GeometricObject {
    private  String color = "white";
    private boolean filled;
    private Date dateCreated;

    //几何图形的无参构造方法
    public GeometricObject(){
        dateCreated = new Date();
    }
    //几何图形的带参构造方法
    public GeometricObject(String color,boolean filled){
        dateCreated = new Date();
        this.color = color;
        this.filled = filled;
    }
    //getter and setter methods
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }
    //描述几何图形
    @Override
    public String toString(){
        return "\ncreated on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
    }
    //抽象方法
    public abstract double getArea();

    public abstract double getPerimeter();
}

public class Circle extends GeometricObject{
    private double radius;

    public Circle(){
    }

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

    public Circle(double radius,String color,boolean filled){
        this.radius = radius;
        setColor(color);
        setFilled(filled);
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return radius * radius * Math.PI;
    }

    public double getDiameter(){
        return 2 * radius;
    }
    public double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    public void printCircle(){
        System.out.println("The circle is created " +
                getDateCreated() + " and the radius is " + radius);
    }

}

public class Retangle extends GeometricObject{
    private double width;
    private double height;

    public Retangle(){

    }

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

    public Retangle(double width, double height,String color, boolean filled){
        this.height = height;
        this.width = width;
        setColor(color);
        setFilled(filled);
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getArea(){
        return width * height;
    }

    public double getPerimeter(){
        return 2 * (width + height);
    }
}
import java.util.Arrays;
public class TestFindLargest {
    public static void main(String[] args) {

        GeometricObject[] geometricObjects = new GeometricObject[8];
        geometricObjects[0] = new NewCircle(4, "Black", true);
        geometricObjects[2] = new NewCircle(1, "Red", true);
        geometricObjects[4] = new NewCircle(9, "Green", true);
        geometricObjects[6] = new NewCircle(8, "Gray", true);
        geometricObjects[7] = new NewCircle(2.5, "Yellow", true);
        geometricObjects[1] = new NewRetangle(9.1, 3, "Red", false);
        geometricObjects[3] = new NewRetangle(7, 13.2, "Blue", false);
        geometricObjects[5] = new NewRetangle(10.1, 2, "White", false);
        double maxGeometricObject = geometricObjects[0].getArea();
        int count = 0;
        for ( int i = 1;i < geometricObjects.length;i++){
            if(maxGeometricObject <= geometricObjects[i].getArea() ){
               maxGeometricObject = geometricObjects[i].getArea() ;
               count = i;
            }
        }
        System.out.println(
                "\nThe largest geometricObject's area is " + maxGeometricObject
                        +geometricObjects[count].toString());
    }
}

 运行结果截图

题目2:

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

定义一个名为ComparableGeometricObjec的类继承GeometricObject和接口 Comparable。重写在列表清单13.2和13.3的Circle和Retangle类。画出UML图及实现这些类。写一个测试程序创创建一个有Circle和Trangle类的实例的数组,将数组排序并显示分类后的元素。

程序设计思路

public interface Comparable {
    public abstract int comparableArea(ComparableGeometricObject object);
    public abstract  int comparablePerimeter(ComparableGeometricObject object);
}
import java.util.Date;

public abstract class GeometricObject {
    private  String color = "white";
    private boolean filled;
    private Date dateCreated;

    //几何图形的无参构造方法
    public GeometricObject(){
        dateCreated = new Date();
    }
    //几何图形的带参构造方法
    public GeometricObject(String color,boolean filled){
        dateCreated = new Date();
        this.color = color;
        this.filled = filled;
    }
    //getter and setter methods
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }
    //描述几何图形
    @Override
    public String toString(){
        return "\ncreated on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
    }
    //抽象方法
    public abstract double getArea();

    public abstract double getPerimeter();
}
public class NewCircle extends ComparableGeometricObject{
    private double radius;

    public NewCircle(){
    }

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

    public NewCircle(double radius,String color,boolean filled){
        this.radius = radius;
        setColor(color);
        setFilled(filled);
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return radius * radius * Math.PI;
    }

    public double getDiameter(){
        return 2 * radius;
    }
    public double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    public void printCircle(){
        System.out.println("The circle is created " +
                getDateCreated() + " and the radius is " + radius);
    }

}
public class NewRetangle extends ComparableGeometricObject{
        private double width;
        private double height;

        public NewRetangle(){

        }

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

        public NewRetangle(double width, double height,String color, boolean filled){
            this.height = height;
            this.width = width;
            setColor(color);
            setFilled(filled);
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }

        public double getArea(){
            return width * height;
        }

        public double getPerimeter(){
            return 2 * (width + height);
        }
    }

public abstract class ComparableGeometricObject extends GeometricObject implements Comparable{
    public abstract double getArea();
    public abstract double getPerimeter();

    @Override
    public int comparableArea(ComparableGeometricObject object){
        if (this.getArea() > object.getArea())
            return 1;
        else if (this.getArea() < object.getArea())
            return -1;
        else
            return 0;
    }
    @Override
    public int comparablePerimeter(ComparableGeometricObject object){
        if (this.getPerimeter() > object.getPerimeter())
            return 1;
        else if (this.getPerimeter() < object.getPerimeter())
            return -1;
        else
            return 0;
    }
}
import java.util.Arrays;
public class TestComparableGeometricObject {
    public static void main(String[] args) {
        ComparableGeometricObject[] geometricObjects = new ComparableGeometricObject[8];
        geometricObjects[0] = new NewCircle(4);
        geometricObjects[2] = new NewCircle(1);
        geometricObjects[4] = new NewCircle(9);
        geometricObjects[6] = new NewCircle(8);
        geometricObjects[7] = new NewCircle(2.5);
        geometricObjects[1] = new NewRetangle(9.1, 3);
        geometricObjects[3] = new NewRetangle(7, 13.2);
        geometricObjects[5] = new NewRetangle(10.1, 2);
        double[] comArea = new double[8];
        for (int i = 0;i < geometricObjects.length;i++){
            comArea[i]=geometricObjects[i].getArea();
            System.out.println("图形信息:" + geometricObjects[i] + "几何图形面积:"+geometricObjects[i].getArea());
        }
        Arrays.sort(comArea);
        System.out.println("\n按照面积排序:");
        for(double object:comArea){
            System.out.println(object);
        }
    }
}

运行结果截图

三、实验结论

通过本次实验实践了类的继承和多态的实现机制,抽象类和接口的用法,得到了接口和抽象类的概念不一样。接口是对动作的抽象,抽象类是对根源的抽象。抽象类不能创建实例,它只能作为父类被继承。抽象类是从多个具体类中抽象出来的父类,它具有更高层次的抽象。从多个具有相同特征的类中抽象出一个抽象类,以这个抽象类作为其子类的模板,从而避免了子类的随意性。接口不能被实例化,接口只能包含方法声明,了解接口和抽象类的区别,感悟子类会继承父类的属性和方法,方法的继承就是原封不动的被继承下来,重载就是在子类中写一个父类方法名一样的,但是参数和返回值等不一样的方法。方法的重写就是和父类的方法框架一模一样,但是内容不一样。抽象类不可以创建对象,抽象类里面一般会有抽象方法。抽象类声明时,只需要在class前加一个abstract。抽象方法所在的类必须是抽象类。多态性就是指父类的某个方法被其子类重写时,可以各自产生自己的功能行为。当一个类有很多子类时,并且这些子类都重写了父类中的某个方法。那么当子类创建的对象的引用放到一个父类的对象中时,就得到了该对象的一个上转型对象,那么这个上转的对象在调用这个方法时就可能具有多种形态;在测试类中的数组相关操作需要多加熟练及接口定义的格式。


网站公告

今日签到

点亮在社区的每一天
去签到