Java2024/src/chapter4/demo45/Rectangle.java
2024-10-23 12:51:53 +08:00

30 lines
577 B
Java

package chapter4.demo45;
/**
* 长方形类
* 必需实现 Shape 接口中的 getArea 和 getPerimeter 方法
*/
public class Rectangle implements Shape {
// 长度
private double length = 0;
// 宽度
private double width = 0;
// 有参构造
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// 获取面积
public double getArea() {
return length * width;
}
// 获取周长
public double getPerimeter() {
return 2 * length + 2 * width;
}
}