要創建一個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類,例如添加其他功能或屬性。