public class Shape {
private double width, height;
/**
* 矩形的构造函数
* @param width 矩形的宽
* @param height 矩形的高
*/
Shape(double width, double height)
{
this.width = width;
this.height = height;
}
/**
* 正方形的构造函数
* @param width 正方形的边长
*/
Shape(double width)
{
this.width = width;
}
/**
* 矩形的计算方法
* @return 矩形的面积
*/
public double calcArea(double width, double height)
{
return width * height;
}
/**
* 重写方法,计算正方形的面积
* @return 正方形的面积
*/
public double calcArea(double width)
{
return width * width;
}
/**
* 测试程序
* @param args
*/
public static void main(String[] args) {
Shape r = new Shape(6,10);
Shape s = new Shape(25);
Shape c = new Shape(4.5);
System.out.println("The area of the rectangle is " + r.calcArea(6, 10));
System.out.println("The area of the square is " + s.calcArea(25));
System.out.println("The area of the circle is " + c.calcArea(4.5));
}
}
Exercise2_partB_4_方法的重载
最新推荐文章于 2024-06-01 10:11:50 发布