定义一个长方形类,有两个属性长和宽,给长和宽定义取值和赋值方法,定义两个构造方法,一个有参构造,使用参数给长和宽赋值,一个无参构造,长和宽的默认值是3和2
定义一个求面积的方法和一个判定是不是正方形的方法。
在测试类中定义一个长方形,判断是不是正方形,并计算面积
class Rectangle {
int length;
int width;
Rectangle(){
this.width = 2;
this.length = 3;
}
Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
int issquare(){
if (this.length ==this.width)
return 1;
return 0;
}
void getArea() {
System.out.println("面积:"+width*length);
}
public static void main(String[] args) {
Rectangle a = new Rectangle(3,3);
if(a.issquare()==1)
a.getArea();
}
}