From abb043731ce7fddd6d81acd01018143eef33fd67 Mon Sep 17 00:00:00 2001 From: Fuyuxinge <1876397977@qq.com> Date: Thu, 2 Apr 2026 14:38:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86w4=E6=96=87=E4=BB=B6=E5=A4=B9=E5=8F=8A?= =?UTF-8?q?=E4=BA=94=E4=B8=AA=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/Circle.java | 23 +++++++++++++++++++++++ w4/Rectangle.java | 34 ++++++++++++++++++++++++++++++++++ w4/Shape.java | 6 ++++++ w4/ShapeUtil.java | 18 ++++++++++++++++++ w4/Triangle.java | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 w4/Circle.java create mode 100644 w4/Rectangle.java create mode 100644 w4/Shape.java create mode 100644 w4/ShapeUtil.java create mode 100644 w4/Triangle.java diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..8bc086c --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,23 @@ +package w4; + +public class Circle extends Shape { + private double radius; + public Circle() {} + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..ed7d813 --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,34 @@ +package w4; + +public class Rectangle extends Shape { + private double length; + private double width; + + public Rectangle() {} + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + @Override + public double getArea() { + return length * width; + } + + public double getLength() { + return length; + } + + public void setLength(double length) { + this.length = length; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..ee731dc --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,6 @@ +package w4; + +public abstract class Shape { + + public abstract double getArea(); +} \ No newline at end of file diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..e2c3b7e --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,18 @@ +package w4; + +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("该图形的面积为:" + shape.getArea()); + } + + public static void main(String[] args) { + Shape circle = new Circle(5); + printArea(circle); + + Shape rectangle = new Rectangle(4, 6); + printArea(rectangle); + + Shape triangle = new Triangle(3, 8); + printArea(triangle); + } +} diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..82b475d --- /dev/null +++ b/w4/Triangle.java @@ -0,0 +1,34 @@ +package w4; + +public class Triangle extends Shape { + private double base; + private double height; + + public Triangle() {} + + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return (base * height) / 2; + } + + public double getBase() { + return base; + } + + public void setBase(double base) { + this.base = base; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } +} \ No newline at end of file