使用继承设计正方形和圆形类

本教程演示了如何使用 Java 继承来设计正方形和圆形类,这些类包含了以下特征:

  • 位置:在平面坐标系中的 x 和 y 坐标
  • 填充颜色:字符串类型
  • 创建日期:Date 类型
  • 正方形的边长:double 类型
  • 圆形的半径:double 类型

每个类都包含以下方法:

  • 访问器和修改器:用于获取和设置类的属性
  • 两个构造方法:一个无参构造方法和一个有参构造方法
  • 计算面积的方法
  • 计算周长的方法
  • 返回其所有属性字符串描述的方法

UML 类图

UML

代码示例

import java.text.SimpleDateFormat;
import java.util.Date;

public class Shape {
    private int x, y;
    private String color;
    private Date createDate;

    public Shape() {
        this.x = 0;
        this.y = 0;
        this.color = 'white';
        this.createDate = new Date();
    }

    public Shape(int x, int y, String color, Date createDate) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.createDate = createDate;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getColor() {
        return color;
    }

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

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return "x = " + x + ", y = " + y + ", color = " + color + ", createDate = " + sdf.format(createDate);
    }
}

public class Square extends Shape {
    private double sideLength;

    public Square() {
        super();
        this.sideLength = 0;
    }

    public Square(int x, int y, String color, Date createDate, double sideLength) {
        super(x, y, color, createDate);
        this.sideLength = sideLength;
    }

    public double getSideLength() {
        return sideLength;
    }

    public void setSideLength(double sideLength) {
        this.sideLength = sideLength;
    }

    public double getArea() {
        return sideLength * sideLength;
    }

    public double getPerimeter() {
        return 4 * sideLength;
    }

    public String toString() {
        return "Square: " + super.toString() + ", sideLength = " + sideLength + ", area = " + getArea() + ", perimeter = " + getPerimeter();
    }
}

public class Circle extends Shape {
    private double radius;

    public Circle() {
        super();
        this.radius = 0;
    }

    public Circle(int x, int y, String color, Date createDate, double radius) {
        super(x, y, color, createDate);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

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

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

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

    public String toString() {
        return "Circle: " + super.toString() + ", radius = " + radius + ", area = " + getArea() + ", perimeter = " + getPerimeter();
    }
}

import java.util.Date;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please input the side length of the square: ");
        double sideLength = input.nextDouble();
        Square s = new Square(0, 0, "red", new Date(), sideLength);
        System.out.println(s.toString());

        System.out.print("Please input the radius of the circle: ");
        double radius = input.nextDouble();
        Circle c = new Circle(0, 0, "blue", new Date(), radius);
        System.out.println(c.toString());

        input.close();
    }
}

示例输出

Please input the side length of the square: 5
Square: x = 0, y = 0, color = red, createDate = 2021-05-17 22:19:57, sideLength = 5.0, area = 25.0, perimeter = 20.0
Please input the radius of the circle: 3
Circle: x = 0, y = 0, color = blue, createDate = 2021-05-17 22:19:57, radius = 3.0, area = 28.274333882308138, perimeter = 18.84955592153876

说明

  • 以上代码示例使用 Java 语言编写,并遵循了 Java 的设计规范和命名规范。
  • 您可以将代码复制到您的 IDE 中进行编译和运行。
  • 您可以根据需要修改代码,例如添加新的属性或方法,以满足您的特定需求。
  • 希望本教程能够帮助您理解 Java 继承的概念,并能够运用继承设计自己的类。
Java 面向对象编程:使用继承设计正方形和圆形类