题目:
设计一个控制台应用程序,采用虚方法求长方形、圆、圆球体和圆柱体的面积或表面积。
程序代码:
自定义class
public class Rectangle //长方形类
{
public const double PI = Math.PI;
protected double x, y;
public Rectangle() { }
public Rectangle(double x1, double y1)
{
x = x1; y = y1;
}
public virtual double Area() //求面积
{
return x * y;
}
}
public class Circle : Rectangle //圆类
{
public Circle(double r) : base(r, 0) { }
public override double Area() //求面积
{
return PI * x * x;
}
}
class Sphere : Rectangle //圆球体类
{
public Sphere(double r) : base(r, 0) { }
public override double Area() //求面积
{
return 4 * PI * x * x;
}
}
class Cylinder : Rectangle //圆柱体类
{
public Cylinder(double r, double h) : base(r, h) { }
public override double Area() //求面积
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}
主函数Main()方法
static void Main(string[] args)
{
double x = 2.4, y = 5.6;
double r = 3.0, h = 5.0;
Rectangle t = new Rectangle(x, y);
Rectangle c = new Circle(r);
Rectangle s = new Sphere(r);
Rectangle l = new Cylinder(r, h);
Console.WriteLine("长为{0}m宽为{1}m的 长方形 面积={2:F2}㎡",x, y, t.Area());
Console.WriteLine("半径为{0}m的 圆 面积={1:F2}㎡", r, c.Area());
Console.WriteLine("半径为{0}m的 圆球体 表面积={1:F2}㎡", r, s.Area());
Console.WriteLine("半径为{0}m高度为{1}m的 圆柱体 表面积={2:F2}㎡", r, h, l.Area());
}
运行结果: