本文将介绍如何使用Java面向对象编程实现形状的周长与面积的计算,通过继承与多态来改进设计,使得代码更加简洁、易于维护。
- 定义抽象类Shape
首先,我们需要定义一个抽象类Shape,该类包含一个不可变静态常量double PI,以及两个抽象方法getPerimeter和getArea,分别用于计算形状的周长和面积。以下是Shape类的代码:
public abstract class Shape {
public static final double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
- 定义Rectangle与Circle类
接下来,我们需要定义两个类Rectangle和Circle,它们都继承自Shape类,并实现getPerimeter和getArea方法。Rectangle类包含一个private width和一个private length属性,表示矩形的宽度和长度;Circle类包含一个private radius属性,表示圆的半径。以下是Rectangle和Circle类的代码:
public class Rectangle extends Shape {
private int width;
private int length;
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public double getPerimeter() {
return 2 * (width + length);
}
public double getArea() {
return width * length;
}
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
public class Circle extends Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * PI * radius;
}
public double getArea() {
return PI * radius * radius;
}
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
- 编写sumAllArea和sumAllPerimeter方法
为了计算形状数组中所有对象的面积和与周长和,我们可以编写两个静态方法sumAllArea和sumAllPerimeter。以下是sumAllArea和sumAllPerimeter方法的代码:
public static double sumAllArea(Shape[] shapes) {
double sum = 0;
for (Shape shape : shapes) {
sum += shape.getArea();
}
return sum;
}
public static double sumAllPerimeter(Shape[] shapes) {
double sum = 0;
for (Shape shape : shapes) {
sum += shape.getPerimeter();
}
return sum;
}
- 在main函数中使用
在main函数中,我们可以先输入一个整数n,表示要输入n个形状。然后,我们可以通过循环读入n行数据,根据输入的形状类型,创建一个Rectangle或Circle对象,并将其放入shapes数组中。接下来,我们可以分别调用sumAllArea和sumAllPerimeter方法,计算所有形状的面积和和周长和,并使用Arrays.toString方法输出所有形状的信息。最后,我们可以分别输出每个形状的类型与父类型。以下是main函数的代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Shape[] shapes = new Shape[n];
for (int i = 0; i < n; i++) {
String type = scanner.next();
if (type.equals("rect")) {
int width = scanner.nextInt();
int length = scanner.nextInt();
shapes[i] = new Rectangle(width, length);
} else if (type.equals("cir")) {
int radius = scanner.nextInt();
shapes[i] = new Circle(radius);
}
}
double sumArea = sumAllArea(shapes);
double sumPerimeter = sumAllPerimeter(shapes);
System.out.println(sumPerimeter);
System.out.println(sumArea);
System.out.println(Arrays.toString(shapes));
for (Shape shape : shapes) {
System.out.println(shape.getClass() + "," + shape.getClass().getSuperclass());
}
}
}
以上代码中,我们先输入了整数n,然后通过循环读入n行数据,根据输入的形状类型,创建一个Rectangle或Circle对象,并将其放入shapes数组中。接下来,我们分别调用sumAllArea和sumAllPerimeter方法,计算所有形状的面积和和周长和,并使用Arrays.toString方法输出所有形状的信息。最后,我们使用for循环遍历shapes数组,分别输出每个形状的类型与父类型。
- 总结
本文介绍了如何使用Java面向对象编程实现形状的周长与面积的计算,通过继承与多态来改进设计,使得代码更加简洁、易于维护。通过学习本文介绍的方法,读者可以进一步掌握Java面向对象编程的基础知识,提高编程能力。
需要注意的是,在读入混合使用nextInt与nextLine的时候,可能会出现行尾回车换行问题,需要使用scanner.nextLine()将行尾回车换行读入并丢弃,以避免出现错误。
完整代码如下:
import java.util.Arrays;
import java.util.Scanner;
public abstract class Shape {
public static final double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape {
private int width;
private int length;
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public double getPerimeter() {
return 2 * (width + length);
}
public double getArea() {
return width * length;
}
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle extends Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * PI * radius;
}
public double getArea() {
return PI * radius * radius;
}
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static double sumAllArea(Shape[] shapes) {
double sum = 0;
for (Shape shape : shapes) {
sum += shape.getArea();
}
return sum;
}
public static double sumAllPerimeter(Shape[] shapes) {
double sum = 0;
for (Shape shape : shapes) {
sum += shape.getPerimeter();
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 读入行尾回车换行
Shape[] shapes = new Shape[n];
for (int i = 0; i < n; i++) {
String type = scanner.next();
if (type.equals("rect")) {
int width = scanner.nextInt();
int length = scanner.nextInt();
shapes[i] = new Rectangle(width, length);
} else if (type.equals("cir")) {
int radius = scanner.nextInt();
shapes[i] = new Circle(radius);
}
scanner.nextLine(); // 读入行尾回车换行
}
double sumArea = sumAllArea(shapes);
double sumPerimeter = sumAllPerimeter(shapes);
System.out.println(sumPerimeter);
System.out.println(sumArea);
System.out.println(Arrays.toString(shapes));
for (Shape shape : shapes) {
System.out.println(shape.getClass() + "," + shape.getClass().getSuperclass());
}
}
}