集册 Java实例教程 Point类的复合矩形类

Point类的复合矩形类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

498
Point类的复合矩形类

class Point {/**来自 n o w  j a v a  . c o m**/

    public int x = 0;

    public int y = 0;


    // a constructor!

    public Point(int x, int y) {

        this.x = x;

        this.y = y;

    }

}


class Rectangle {

    /** The width of this rectangle. */

    public int width;


    /** The height of this rectangle. */

    public int height;


    /** The origin (lower-left corner) of this rectangle. */

    public Point origin;

    /**
    N o w  J a v a  .   c o m
    **/

    /**

     * Creates a rectangle with the specified size.

     * The origin is (0, 0).

     * @param w width of the rectangle

     * @param h height of the rectangle

     */

    public Rectangle(int w, int h) {

        this(new Point(0, 0), w, h);

    }


    /**

     * Creates a rectangle with the specified origin and

     * size.

     * @param p origin of the rectangle

     * @param w width of the rectangle

     * @param h height of the rectangle

     */

    public Rectangle(Point p, int w, int h) {

        origin = p;

        width = w;

        height = h;

    }


    /**

     * Moves the rectangle to the specified origin.

     */

    public void move(int x, int y) {

        origin.x = x;

        origin.y = y;

    }


    /**

     * Returns the computed area of the rectangle.

     */

    public int area() {

        return width * height;

    }

}


展开阅读全文