集册 Java实例教程 重载构造函数

重载构造函数

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

574
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
重载构造函数
/**
来 自 时 代      J a v a   公   众 号 - nowjava.com
**/

import java.awt.Point;


public class Main {

    int x1 = 0;

    int y1 = 0;

    int x2 = 0;

    int y2 = 0;


    Main(int x1, int y1, int x2, int y2) {

        this.x1 = x1;

        this.y1 = y1;

        this.x2 = x2;

        this.y2 = y2;

    }


    Main(Point topLeft, Point bottomRight) {

        this(topLeft.x, topLeft.y, bottomRight.x,/**来 自 时 代 J     a    v  a - nowjava.com**/

            bottomRight.y);

    }


    Main(Point topLeft, int w, int h) {

        this(topLeft.x, topLeft.y, topLeft.x + w,

            topLeft.y + h);

    }


    void printBox() {

        System.out.print("Box: <" + x1 + ", " + y1);

        System.out.println(", " + x2 + ", " + y2 + ">");

    }


    public static void main(String[] arguments) {

        Main rect;


        System.out.println("Calling Box2 with coordinates "

            + "(25,25) and (50,50):");

        rect = new Main(25, 25, 50, 50);

        rect.printBox();


        System.out.println("\nCalling Box2 with points "

            + "(10,10) and (20,20):");

        rect= new Main(new Point(10, 10), 
展开阅读全文