создание объекта другого класса и установка того же имени объекта, что и имя метода, почему? - PullRequest
0 голосов
/ 09 апреля 2020

Я создаю программу для проверки перекрытия двух треугольников. В этом первом я создаю класс MyRectangle, который принимает треугольники x, y, ширину и высоту.

другой класс является основным классом, где я создаю метод, который принимает 2 прямоугольника в качестве параметров, в первую очередь здесь я вызываю класс MyRectangle и создаю объект 2 раза для получения размеров двух треугольников, а затем передаю параметры x, y, widht, height

во-вторых, я создаю имя метода MyRectangle findOverlapmethod (MyRectangle r1, MyRectangle r2) вот так. Мой вопрос заключается в том, почему мы создаем имя метода, совпадающее с именем объекта.

Ниже приведен мой код, МОЙ первый класс:

publi c класс MyRectangle {

int width;
int height;
int x;
int y;

public MyRectangle(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

public String toString() {
    return "x:" + this.x + " y:" + this.y + " width:" + this.width + " height:" + this.height;
}

}

Другой класс: пакет Overlap;

publi c класс RectangleOverlap {

public static void main(String[] args) {

    MyRectangle r1 = new MyRectangle(7, 7, 10, 10);
    System.out.println("r1[" + r1 +"]");
    MyRectangle r2 = new MyRectangle(4, 6, 8, 10);
    System.out.println("r2[" + r2 + "]");

    System.out.println(findOverlap(r1, r2));
}

static MyRectangle findOverlap(MyRectangle r1, MyRectangle r2) {
    System.out.println("Came inside myRectangle");
    int overlapX;
    int overlapY;
    int overlapWidth;
    int overlapHeight;

    MyRectangle leftRect;
    MyRectangle rightRect;

    MyRectangle upperRect;
    MyRectangle lowerRect;

    // determine the left and right
    if (r1.x <= r2.x) {
        leftRect = r1;
        rightRect = r2;
    } else {
        leftRect = r2;
        rightRect = r1;
    }

    // determine upper and lower
    if (r1.y <= r2.y) {
        upperRect = r1;
        lowerRect = r2;
    } else {
        upperRect = r2;
        lowerRect = r1;
    }

    // calculate the overlapWidth
    if (leftRect.x + leftRect.width <= rightRect.x) {
        // no overlap
        return null;
    } else if (leftRect.x + leftRect.width >= rightRect.x + rightRect.width) {
        // fully overlapped
        overlapWidth = rightRect.width;
    } else {
        // partial overlap
        overlapWidth = leftRect.x + leftRect.width - rightRect.x;
    }

    // calculate the overlapHeight
    if (upperRect.y + upperRect.height <= lowerRect.y) {
        // no overlap
        return null;
    } else if (upperRect.y + upperRect.height >= lowerRect.y + lowerRect.height) {
        // fully overlap
        overlapHeight = lowerRect.height;
    } else {
        // partial overlap
        overlapHeight = upperRect.y + upperRect.height - lowerRect.y;
    }

    overlapX = rightRect.x;
    overlapY = lowerRect.y;

    return new MyRectangle(overlapX, overlapY, overlapWidth, overlapHeight);
}
* * 1 020}
...