在Java中,你可以创建一个名为Rectangle的类,包含宽度(width)和高度(height)作为成员变量,并定义area()和perimeter()方法来计算矩形的面积和周长。以下是一个简单的实现: ```java public class Rectangle { private double width; private double height; // 构造函数 public Rectangle(double width, double height) { this.width = width; this.height = height; } // Getter and Setter methods public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } // 计算面积的方法 public double area() { return width * height; } // 计算周长的方法 public double perimeter() { return 2 * (width + height); } // 如果你想计算圆形的周长,你可以创建一个Circle类,因为矩形和圆是两种不同的形状 // 但是在这个Rectangle类中,我们不需要这个方法,因为我们主要关注矩形 // 如果你需要,可以创建一个Circle类,如下所示: // public static double circlePerimeter(double radius) { // return 2 * Math.PI * radius; // } } ``` 现在,你可以创建一个Rectangle对象并使用它的方法: ```java public class Main { public static void main(String[] args) { Rectangle rectangle = new Rectangle(5, 10); System.out.println("Area: " + rectangle.area()); System.out.println("Perimeter: " + rectangle.perimeter()); } } ``` 这将输出: ``` Area: 50.0 Perimeter: 30.0 ``` [2024-04-11 15:15:25 | AI写代码神器 | 398点数解答]