class Demo3_Rectangle {
public static void main(String[] args) {
Rectangle r=new Rectangle(20,30);
System.out.println(r.getLength());
System.out.println(r.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){ //给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 (width+high)*2;
}
public int getArea(){
return width*high;
}
}
|
|