public class Rectangle {
private int length;
private int width;
public Rectangle(){
}
public void setA(int length,int width){
this.length=length;
this.width=width;
}
public int getA(){
return this.length;
}
public int getA1(){
return this.width;
}
public int area(){
return length*width;
}
public int perimeter(){
return 2*(length+width);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rectangle=new Rectangle();
rectangle.setA(2,3);
rectangle.getA();
rectangle.getA1();
rectangle.area();
rectangle.perimeter();
System.out.println("面积是"+rectangle.area());
System.out.println("周长是"+rectangle.perimeter());
}
}
用get和set取值来计算长方形的周长和面积(包括成员方法和构造方法)
最新推荐文章于 2023-01-04 16:54:54 发布