定义一个矩形类rectangle,类中有两个属性,即长length、宽width;一个构造方法rectangle(int width,int length ),可分别给这两个属性赋值;一个方法getArea(),可求面积。在测试类中,创建一个Rectangle对象,计算矩形面积并输出
public class Rectangle {
private int length;
private int width;
public Rectangle(int length,int width) {
this.length=length;
this.width=width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public static void getArea(int length,int width) {
int area=width*length;
System.out.println("矩形的面积为:"+area);
}
public static void main(String[] args) {
System.out.println("请输入矩形的长和宽:");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
getArea(a,b);
}
}