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.
35 lines
788 B
35 lines
788 B
// 矩形类,继承抽象类Shape
|
|
public class Rectangle extends Shape {
|
|
// 长、宽
|
|
private double length;
|
|
private double width;
|
|
|
|
// 构造方法:初始化长和宽
|
|
public Rectangle(double length, double width) {
|
|
this.length = length;
|
|
this.width = width;
|
|
}
|
|
|
|
// 重写getArea方法:矩形面积 = 长 * 宽
|
|
@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;
|
|
}
|
|
}
|