class Demo_4 {
public static void main(String[] args) {
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 setHeigth(int height){ //设置长
this.height = height;
}
public int getLength(){ //获取周长的方法
return (width+height)*2;
}
public int getArea(){
return width*height;
}
} |
|