- class Test {
- public static void main(String[] args) {
- Rectangle r = new Rectangle(10,20);
- System.out.println(r.getLength());
- System.out.println(r.getArea());
- System.out.println("--------------");
- Rectangle r2 = new Rectangle();
- r2.setWidth(15);
- r2.setHigh(20);
- System.out.println(r2.getLength());
- System.out.println(r2.getArea());
- }
- }
- class Rectangle{
- private int width;
- private int high;
- public Rectangle (){} //空参构造
-
- public Rectangle (int width,int high){ //带参构造
- this.width = width;
- this.high = high;
- }
- public void setWidth(int width){ //设置宽度
- this.width = width;
- }
- public int getWidth(){ //获取宽度
- return width;
- }
-
- public void setHigh(int high){ //设置高度
- this.high = high;
- }
- public int getHigh(){ //获取高度
- return high;
- }
-
- public int getLength(){ //计算周长
- return 2*(width + high);
- }
- public int getArea(){ //计算面积
- return width * high;
- }
- }
复制代码 |
|