Java怎么用面对对象的方法输出矩形转置

Java怎么用面对对象的方法输出矩形转置,刚刚学了面对对象,要求我们用面对对象的方法输出矩形转置,不太明白应该怎么做,希望有人可以解答一下,谢谢,下图这种样式的

img

输出成这种

img

可以使用面向对象的方法来实现矩形转置。可以定义一个 Rectangle 类来表示矩形,其中包括矩形的长和宽。然后在 Rectangle 类中定义一个 transpose() 方法来进行矩形的转置操作。

public class Rectangle {
    private int width;
    private int height;

    public Rectangle(int w, int h) {
        this.width = w;
        this.height = h;
    }

    public void transpose() {
        int temp = this.width;
        this.width = this.height;
        this.height = temp;
    }

    public String toString() {
        return "Width: " + width + ", Height: " + height;
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(5, 10);
        System.out.println(rect); // 输出原始矩形的宽和高

        rect.transpose(); // 转置矩形
        System.out.println(rect); // 输出转置后矩形的宽和高
    }
}