You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
/**
|
|
* 矩形类,继承自抽象图形类Shape
|
|
* 实现矩形的面积计算逻辑:面积 = 长 * 宽
|
|
*/
|
|
public class Rectangle extends Shape {
|
|
// 矩形的长和宽
|
|
private double length;
|
|
private double width;
|
|
|
|
/**
|
|
* 构造方法:初始化矩形的长和宽
|
|
* @param length 长(需大于0)
|
|
* @param width 宽(需大于0)
|
|
*/
|
|
public Rectangle(double length, double width) {
|
|
if (length <= 0 || width <= 0) {
|
|
throw new IllegalArgumentException("长和宽必须大于0!");
|
|
}
|
|
this.length = length;
|
|
this.width = width;
|
|
}
|
|
|
|
/**
|
|
* 实现抽象方法:计算矩形面积
|
|
* @return 矩形的面积
|
|
*/
|
|
@Override
|
|
public double getArea() {
|
|
return length * width;
|
|
}
|
|
|
|
// Getter/Setter方法(可选)
|
|
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;
|
|
}
|
|
}
|
|
|