尝试编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长。宽初始化,定义一个成员方法求此矩形的面积。

public class Area{
private int length;
private int width;
public Area(int length,int width){
this.length=length;
this.width=width;
}
public int getArea(){//计算面积
return length*width;
}
public static void main(String[] args){
Area a=new Area(20,10);
int area1=a.getArea();//计算第一个长方形面积
System.out.println("第一个长方形的面积为:"+area1);
Area b=new Area(6,3);//计算第二个长方形面积
int area2=b.getArea();
System.out.println("第二个长方形的面积为:"+area2);
}
}

 

posted @ 2016-03-04 21:20  边晓艳  阅读(8087)  评论(0编辑  收藏  举报