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.

62 lines
1.2 KiB

package com.trae.shape;
/**
* 矩形类
* 继承自Shape抽象类,实现了getArea()方法
*/
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 矩形的长
*/
public double getLength() {
return length;
}
/**
* 设置矩形的长
* @param length 矩形的长
*/
public void setLength(double length) {
this.length = length;
}
/**
* 获取矩形的宽
* @return 矩形的宽
*/
public double getWidth() {
return width;
}
/**
* 设置矩形的宽
* @param width 矩形的宽
*/
public void setWidth(double width) {
this.width = width;
}
/**
* 计算矩形的面积
* 面积公式:长 × 宽
* @return 矩形的面积
*/
@Override
public double getArea() {
return length * width;
}
}