- public class Rectangle {
- // Rectangle : 矩形
- private double length = 0.0; // 长 属性
- private double width = 0.0; // 宽 属性
- 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;
- }
- // 自定义 计算周长方法
- public double circumference() {
- return (this.length + this.width) * 2;
- }
- // 自定义 计算面积方法
- public double area() {
- return this.length * this.width;
- }
- // 入口方法
- public static void main(String[] args) {
- Rectangle rect = new Rectangle();
- rect.setLength(3.0);
- rect.setWidth(5.0);
- System.out.println("周长为:" + rect.circumference());
- System.out.println("面积为:" + rect.area());
- }
- }
复制代码 |