rectangle1.java

来自「这是《Java2程序设计实用教程(第2版)》教材中附带的例题源代码。」· Java 代码 · 共 52 行

JAVA
52
字号
//【例3.8】  抽象类与抽象方法。

public class Rectangle1 extends PlaneGraphics1 //长方形类
{
    protected double length;           //长度
    protected double width;            //宽度

    public Rectangle1(double length, double width)
    {                                  //构造方法
        super("长方形");
        this.length = length;
        this.width = width;
    }

    public Rectangle1(double width)    //正方形是长方形的特例
    {
        super("正方形");
        this.length = width;
        this.width = width;
    }

    public Rectangle1()
    {
        this(0,0);
    }

    public void setLength(double length)
    {
        this.length = length;
    }

    public void setWidth(double width)
    {
        this.width = width;
    }

    public double getLength()
    {
        return this.length;
    }

    public double getWidth()
    {
        return this.width;
    }

    public double area()               //计算长方形面积,实现父类的抽象方法
    {
        return this.width * this.length;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?