
编写一个java程序,求解答,谢谢。
1个回答
2019-03-14
展开全部
public class Box
{
private int length;
private int width;
public Box()
{
super();
}
public Box(int length, int width)
{
super();
this.length = length;
this.width = width;
}
public int getS()
{
int area = this.length * this.width; // 面积等于长乘宽
return area;
}
public int getL()
{
int perimeter = 2 * (this.length + this.width);
return perimeter;
}
public static void main(String[] args)
{
// 有参数的方法
Box b1 = new Box(2, 3);
System.out.println("面积1=" + b1.getS());
System.out.println("周长1=" + b1.getL());
// 无参数的方法
Box b2 = new Box();
System.out.println("面积2=" + b2.getS());
System.out.println("周长2=" + b2.getL());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询