この記事では、Java オブジェクト指向プログラミングを使用して形状の周囲と面積を計算し、継承とポリモーフィズムを通じて設計を改善して、コードをより簡潔にして保守しやすくする方法を紹介します。
- 抽象クラス Shape を定義する
まず、不変の静的定数 double PI を含む抽象クラス Shape と、形状の周長と面積をそれぞれ計算するために使用される 2 つの抽象メソッド getPerimeter と getArea を定義する必要があります。以下は Shape クラスのコードです。
public abstract class Shape {
public static final double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
- Rectangle クラスと Circle クラスを定義する
次に、2 つのクラス Rectangle と Circle を定義する必要があります。これらはどちらも Shape クラスから継承し、getPerimeter メソッドと getArea メソッドを実装します。Rectangle クラスには、長方形の幅と長さを表すプライベート width プロパティとプライベート length プロパティが含まれ、Circle クラスには、円の半径を表すプライベート 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 メソッドを作成する
形状配列内のすべてのオブジェクトの面積と周囲長の合計を計算するには、2 つの静的メソッド 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 ループを使用してシェイプ配列を走査し、各シェイプのタイプと親のタイプをそれぞれ出力します。
- 要約する
この記事では、Java オブジェクト指向プログラミングを使用して形状の周囲と面積を計算する方法を紹介し、継承とポリモーフィズムによって設計を改善し、コードをより簡潔にして保守しやすくします。この記事で紹介する方法を学ぶことで、Java オブジェクト指向プログラミングの基礎知識をさらに習得し、プログラミング能力を向上させることができます。
nextInt と nextLine を組み合わせて読み取る場合、行末のキャリッジ リターンとライン フィードの問題が発生する可能性があることに注意してください。最後のキャリッジ リターンとライン フィードを読み取るには、scanner.nextLine() を使用する必要があります。エラーを避けるために、その行を破棄してください。
完全なコードは次のとおりです。
java.util.Arrays をインポートします。
java.util.Scannerをインポートします。
public abstract class Shape { public static Final double PI = 3.14;
public abstract double getPerimeter();
パブリック抽象ダブルgetArea();
}
class Rectangle extends Shape { private int width; プライベート int の長さ。
public Rectangle(int width, int length) { this.width = width; this.length = 長さ; }
public double getPerimeter() { return 2 * (幅 + 長さ); }
public double getArea() { 幅 * 長さを返します。 }
public String toString() { return "Rectangle [width=" + width + ", length=" + length + "]"; } }
クラス Circle extends Shape { private int radius;
public Circle(int radius) { this.radius = radius; }
public double getPerimeter() { 2 * PI * 半径を返します。 }
public double getArea() { PI * 半径 * 半径を返します。 }
public String toString() { return "Circle [radius=" + radius + "]"; } }
public class Main { public static double sumAllArea(Shape[]シェイプ) { double sum = 0; for (シェイプ シェイプ : シェイプ) { sum += シェイプ.getArea(); 合計を返します 。 }
public static double sumAllPerimeter(Shape[]シェイプ) { double sum = 0; for (シェイプ シェイプ : シェイプ) { 合計 += シェイプ.getPerimeter(); 合計を返します 。 }
public static void main(String[] args) { Scanner sinner = new Scanner(System.in); int n = scanner.nextInt(); Scanner.nextLine(); // 読み取り行末に入力して改行
Shape[] シェイプ = 新しい Shape[n];
for (int i = 0; i < n; i++) { 文字列型 = scanner.next(); if (type.equals("rect")) { int width = scanner.nextInt(); int 長 = scanner.nextInt(); 形状[i] = 新しい長方形(幅, 長さ); else if (type.equals("cir")) { int radius = Scanner.nextInt(); 形状[i] = 新しい円(半径); スキャナー .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 形状 : 形状) { System.out.println(shape.getClass() + "," +shape.getClass().getSuperclass()); } } }