要创建一个Rectangle类,可以按照以下步骤进行:
下面是一个示例代码,展示了如何在Java中创建一个Rectangle类:
public class Rectangle {
private double width;
private double height;
public Rectangle() {
this.width = 0;
this.height = 0;
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
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 getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
}
你可以根据需要进行修改和扩展这个Rectangle类,例如添加其他功能或属性。