定义一个长方形(Rectangle)类,定义求周长(length)和面积(area)的方法,
然后定义一个测试类Test,进行测试。
长方形类
public class Rectangle {
private int wide;
private int longg;
public Rectangle() { //无参构造方法
}
public Rectangle(int wide, int longg) { //有参构造方法
this.wide = wide;
this.longg = longg;
}
public int getWide() {
return wide;
}
public void setWide(int wide) {
this.wide = wide;
}
public int getLongg() {
return longg;
}
public void setLongg(int longg) {
this.longg = longg;
}
}
测试类Test
public class Test { //定义Test测试类
public static void main(String[] args) {
Rectangle r = new Rectangle(); //无参构造方法
r.setLongg(15);
r.setWide(30);
Rectangle r1 = new Rectangle(20,16);//有参构造方法
area(r);
area(r1);
c(r);
c(r1);
}
public static void area(Rectangle a){ //定义计算面积的方法
int area=a.getLongg()*a.getWide();
System.out.println("面积是:"+area);
}
public static void c(Rectangle a){ //定义计算周长的面积
int c=2*(a.getWide()+a.getLongg());
System.out.println("周长是:"+c);
}
}
运行结果



