定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类,进行测试。
~~
Rectangle r1 = new Rectangle(2, 3);
System.out.println(“周长是:” + r1.getLength() + " " + “面积是:” + r1.getArea());
r1.setWidth(5);
r1.setHeight(6);
System.out.println(“周长是:” + r1.getLength() + " " + “面积是:” + r1.getArea());
}
}
class Rectangle {
private int width;
private int height;
public Rectangle() {
}
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getLength() {
return (width + height) * 2;
}
public int getArea() {
return width * height;
}
1234567891011121314151617
}