abstract class Shape { public abstract double area(); } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } } class Rectangle extends Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } @Override public double area() { return length * width; } } class Triangle extends Shape { private double base; private double height; public Triangle(double base, double height) { this.base = base; this.height = height; } @Override public double area() { return 0.5 * base * height; } } public class PolymorphismDemo { public static void main(String[] args) { Shape[] shapes = new Shape[3]; shapes[0] = new Circle(2.0); shapes[1] = new Rectangle(3.0, 4.0); shapes[2] = new Triangle(2.0, 3.0); double totalArea = 0.0; for (Shape shape : shapes) { totalArea += shape.area(); } System.out.println("Total area: " + totalArea); } }
时间: 2023-12-15 17:45:30 浏览: 112
这段代码展示了多态的概念。Shape是一个抽象类,定义了一个抽象方法area(),表示形状的面积。Circle、Rectangle和Triangle是Shape的子类,它们都实现了area()方法,并分别表示圆、矩形和三角形。在主方法中,创建了一个Shape类型的数组shapes,并将Circle、Rectangle和Triangle的实例存储到数组中。然后,遍历shapes数组,对每个Shape对象调用area()方法,计算总面积。由于Circle、Rectangle和Triangle都是Shape的子类,它们的area()方法的实现不同,因此实现了多态的效果。
相关问题
能补充这段代码吗import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Polygon; import javafx.stage.Stage; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; abstract class Shape { protected Color color; protected double area; protected double perimeter; protected double positionX; protected double positionY; public abstract void calculateArea(); public abstract void calculatePerimeter(); public abstract void draw(Pane pane); public void setPosition(double x, double y) { this.positionX = x; this.positionY = y; } } class CircleShape extends Shape { private double radius; public CircleShape(double radius) { this.radius = radius; this.color = Color.RED; } @Override public void calculateArea() { this.area = Math.PI * Math.pow(radius, 2); } @Override public void calculatePerimeter() { this.perimeter = 2 * Math.PI * radius; } @Override public void draw(Pane pane) { Circle circle = new Circle(radius); circle.setFill(color); circle.setLayoutX(positionX); circle.setLayoutY(positionY); pane.getChildren().add(circle); } } class RectangleShape extends Shape { private double width; private double height; public RectangleShape(double width, double height) { this.width = width; this.height = height; this.color = Color.BLUE; } @Override public void calculateArea() { this.area = width * height; } @Override public void calculatePerimeter() { this.perimeter = 2 * (width + height); } @Override public void draw(Pane pane) { Rectangle rectangle = new Rectangle(width, height); rectangle.setFill(color); rectangle.setLayoutX(positionX); rectangle.setLayoutY(positionY); pane.getChildren().add(rectangle); } } class TriangleShape extends Shape { private double[] points; public TriangleShape(double[] points) { this.points = points; this.color = Color.GREEN; } @Override public void calculateArea() { double x1 = points[0]; double y1 = points[1]; doubl
e x2 = points[2]; double y2 = points[3]; double x3 = points[4]; double y3 = points[5]; this.area = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); } @Override public void calculatePerimeter() { double side1 = Math.sqrt(Math.pow(points[0] - points[2], 2) + Math.pow(points[1] - points[3], 2)); double side2 = Math.sqrt(Math.pow(points[2] - points[4], 2) + Math.pow(points[3] - points[5], 2)); double side3 = Math.sqrt(Math.pow(points[4] - points[0], 2) + Math.pow(points[5] - points[1], 2)); this.perimeter = side1 + side2 + side3; } @Override public void draw(Pane pane) { Polygon triangle = new Polygon(points); triangle.setFill(color); triangle.setLayoutX(positionX); triangle.setLayoutY(positionY); pane.getChildren().add(triangle); } } public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Pane pane = new Pane(); Properties prop = new Properties(); try { FileInputStream input = new FileInputStream("config.properties"); prop.load(input); input.close(); } catch (IOException ex) { ex.printStackTrace(); } for (int i = 1; i <= 3; i++) { String shapeType = prop.getProperty("shape" + i + ".type"); Shape shape; if (shapeType.equals("circle")) { double radius = Double.parseDouble(prop.getProperty("shape" + i + ".radius")); shape = new CircleShape(radius); } else if (shapeType.equals("rectangle")) { double width = Double.parseDouble(prop.getProperty("shape" + i + ".width")); double height = Double.parseDouble(prop.getProperty("shape" + i + ".height")); shape = new RectangleShape(width, height); } else { double[] points = new double[6]; for (int j = 0; j < 6; j++) { points[j] = Double.parseDouble(prop.getProperty("shape" + i + ".point" + (j+1))); } shape = new TriangleShape(points); } shape.calculateArea(); shape.calculatePerimeter(); double posX = Double.parseDouble(prop.getProperty("shape" + i + ".posX")); double posY = Double.parseDouble(prop.getProperty("shape" + i + ".posY")); shape.setPosition(posX, posY); shape.draw(pane); } Scene scene = new Scene(pane, 500, 500); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
这段代码实现了一个基于JavaFX的图形绘制应用,它定义了一个抽象类 Shape 和三个子类 CircleShape、RectangleShape 和 TriangleShape,分别对应圆形、矩形和三角形,每个子类都实现了抽象方法 calculateArea()、calculatePerimeter() 和 draw()。这些方法用于计算每个图形的面积、周长和绘制图形。在主类 Main 中,从配置文件 config.properties 中读取每个图形的类型、参数和位置,然后创建相应的图形对象并调用其方法进行计算和绘制。最后将绘制结果显示在 JavaFX 的场景中。
写出代码// 定义抽象类 Shape 表示几何图形有哪些共同的属性和方法 public abstract class Shape { protected int x, y; public Shape(int x, int y) { this.x = x; this.y = y; } public abstract double getArea(); // 抽象方法,由子类具体实现 public abstract double getPerimeter(); // 抽象方法,由子类具体实现 public void move(int dx, int dy) { this.x += dx; this.y += dy; } } // 定义矩形 Rectangle 类继承自 Shape 类,并重写 Shape 中的抽象方法 public class Rectangle extends Shape { private int width, height; public Rectangle(int width, int height, int x, int y) { super(x, y); this.width = width; this.height = height; } @Override public double getArea() { return width * height; } @Override public double getPerimeter() { return 2 * (width + height); } } // 定义圆形 Circle 类继承自 Shape 类,并重写 Shape 中的抽象方法 public class Circle extends Shape { private int radius; public Circle(int radius, int x, int y) { super(x, y); this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getPerimeter() { return 2 * Math.PI * radius; } } // 使用多态性,创建一个 Shape 对象的列表,包含 Rectangle 和 Circle 对象 public class Main { public static void main(String[] args) { Shape[] shapes = {new Rectangle(3, 4, 0, 0), new Circle(5, 0, 0)}; for (Shape shape : shapes) { // 调用通用的方法,因为 Rectangle 和 Circle 都是 Shape 类的子类 System.out.printf("Area: %.2f, Perimeter: %.2f\n", shape.getArea(), shape.getPerimeter()); // 调用和重写抽象类中的方法会自动调用对应子类的方法 shape.move(1, 1); System.out.printf("New position: (%d, %d)\n", shape.x, shape.y); } } }的程序流程图
抱歉,我无法直接在此回答中插入程序流程图。但是,以下是程序的大致流程:
1. 定义抽象类 Shape,包含坐标 x 和 y,以及抽象方法 getArea() 和 getPerimeter(),还有一个通用的方法 move()。
2. 定义 Rectangle 类和 Circle 类,分别继承 Shape 类,并实现抽象方法 getArea() 和 getPerimeter()。
3. 在 Main 类中创建一个 Shape 对象的数组,包含 Rectangle 和 Circle 对象。
4. 使用 for-each 循环遍历数组中的每个对象。
5. 对每个对象调用通用的方法 move(),并输出新的位置坐标。
6. 对每个对象调用抽象方法 getArea() 和 getPerimeter(),并输出结果。由于 Rectangle 和 Circle 分别重写了这两个方法,因此调用时会自动调用对应子类的方法。
7. 程序结束。
阅读全文