(3) 定义一个主类Test,分别用三种构造方法创建三个Rectangl对象。 a) 显示x、y、length和width的值。 b) 计算面积并显示。
时间: 2024-05-29 08:10:06 浏览: 14
public class Test {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle(3, 4);
Rectangle rectangle3 = new Rectangle(2, 5, 6, 8);
System.out.println("rectangle1: x=" + rectangle1.x + ", y=" + rectangle1.y + ", length=" + rectangle1.length + ", width=" + rectangle1.width);
System.out.println("rectangle2: x=" + rectangle2.x + ", y=" + rectangle2.y + ", length=" + rectangle2.length + ", width=" + rectangle2.width);
System.out.println("rectangle3: x=" + rectangle3.x + ", y=" + rectangle3.y + ", length=" + rectangle3.length + ", width=" + rectangle3.width);
System.out.println("rectangle1 area: " + rectangle1.getArea());
System.out.println("rectangle2 area: " + rectangle2.getArea());
System.out.println("rectangle3 area: " + rectangle3.getArea());
}
}
class Rectangle {
int x;
int y;
int length;
int width;
public Rectangle() {
this.x = 0;
this.y = 0;
this.length = 0;
this.width = 0;
}
public Rectangle(int length, int width) {
this.x = 0;
this.y = 0;
this.length = length;
this.width = width;
}
public Rectangle(int x, int y, int length, int width) {
this.x = x;
this.y = y;
this.length = length;
this.width = width;
}
public int getArea() {
return length * width;
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)