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.
52 lines
1.0 KiB
52 lines
1.0 KiB
/**
|
|
* 矩形类,继承自 Shape
|
|
* 实现矩形的面积计算和绘制功能
|
|
*/
|
|
public class Rectangle extends Shape {
|
|
// 矩形的长和宽
|
|
private double length;
|
|
private double width;
|
|
|
|
/**
|
|
* 构造方法:初始化矩形
|
|
* @param length 长
|
|
* @param width 宽
|
|
*/
|
|
public Rectangle(double length, double width) {
|
|
this.length = length;
|
|
this.width = width;
|
|
}
|
|
|
|
/**
|
|
* 实现父类方法:计算矩形面积
|
|
* @return 面积
|
|
*/
|
|
@Override
|
|
public double getArea() {
|
|
return length * width;
|
|
}
|
|
|
|
/**
|
|
* 实现父类方法:绘制矩形
|
|
*/
|
|
@Override
|
|
public void draw() {
|
|
System.out.println("正在绘制一个矩形,长:" + length + ",宽:" + width);
|
|
}
|
|
|
|
/**
|
|
* 获取长度
|
|
* @return 长度值
|
|
*/
|
|
public double getLength() {
|
|
return length;
|
|
}
|
|
|
|
/**
|
|
* 获取宽度
|
|
* @return 宽度值
|
|
*/
|
|
public double getWidth() {
|
|
return width;
|
|
}
|
|
}
|
|
|