一、实验目的
- 理解面向对象程序的基本概念。
- 掌握类的继承和多态的实现机制。
- 熟悉抽象类和接口的用法。
二、实验内容、过程及结果
**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;}@Overridepublic 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;}@Overridepublic 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() {}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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());}}}
}
运行结果
三、实验结论
通过本次实验实践了类的继承和多态的实现机制的知识和操作,熟悉了抽象类和接口的用法得到了理论要与实践相结合的感悟,在实践操作方面可以再努努力!
结语
你都不知道山有多高
你怎么知道终点在哪
!!!