分析:
分别使用三种方法实现
- 类的继承与虚函数,即多态性;
- 抽象类继承;
- 接口。
第一种方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shangji1_03
{
public class Circle //基类
{
protected double radius;
public Circle(double r) { radius = r; }
public virtual double GetArea()
{ return Math.PI * radius * radius; }
public virtual double GetVolumn() { return 0.0; }
}
public class Sphere : Circle//球体类
{
public Sphere(double r) : base(r) { }
public override double GetArea()
{ return (4 * base.GetArea()); }
public override double GetVolumn()
{ return (4 * Math.PI * Math.Pow(radius, 3) / 3); }
}
public class Cylinder : Circle//圆柱类
{
private double height;//添加高度字段
public Cylinder(double r, double h) : base(r) { height = h; }
public override double GetArea()
{ return (2 * base.GetArea() + 2 * Math.PI * radius * height); }
public override double GetVolumn()
{ return (Math.PI * radius * radius * height); }
}
public class Cone : Circle//圆锥类
{
private double height;//添加高度字段
public Cone(double r, double h) : base(r) { height = h; }
public override double GetArea()
{
return (Math.PI * radius *
(radius + Math.Sqrt(height * height + radius * radius)));
}
public override double GetVolumn()
{ return (Math.PI * radius * radius * height / 3); }
}
class Program
{
static void Main(string[] args)
{
Circle[] c = new Circle[3];//建立基类数组
c[0] = new Sphere(2);
c[1] = new Cylinder(2, 10);
c[2] = new Cone(2, 20);
for (int i = 0; i < 3; ++i)
{
Console.WriteLine("c[{0}]’s surface area={1}, volumn={2}",
i, c[i].GetArea(), c[i].GetVolumn());
}
Console.ReadKey();
}
}
}
第二种方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shangji1_03
{
public abstract class Shape
{
public const double PI = 3.1415926;
public abstract double getArea(); //求面积
public abstract double getVol(); //求体积
}
public class Sphere : Shape
{
private double r; //半径
public Sphere(double r)
{
// TODO: Complete member initialization
this.r = r;
}
public override double getArea()
{
double area = 0;
area = 4*PI * r * r;
return area;
}
public override double getVol()
{
double vol = 0;
vol = 4 / 3 * PI * r * r * r;
return vol;
}
}
public class Cylinder : Shape
{
private double r; //底面半径
private double h; //圆柱的高
public Cylinder(double r, double h)
{
// TODO: Complete member initialization
this.r = r;
this.h = h;
}
public override double getArea()
{
double area;
area = 2 * PI * r * r + 2 * PI * r * h;
return area;
}
public override double getVol()
{
double vol;
vol = PI * r * r * h;
return vol;
}
}
public class Cone : Shape
{
private double r; //底面半径
private double l; //圆锥的母线
public Cone(double r, double l)
{
// TODO: Complete member initialization
this.r = r;
this.l = l;
}
public override double getArea()
{
double area;
area = PI*r*r+PI*r*l;
return area;
}
public override double getVol()
{
double vol;
vol = 1 / 3.0d * PI * r * r * Math.Sqrt(l * l - r * r);
return vol;
}
}
class Program
{
static void Main(string[] args)
{
double r = 3.0d, h = 4.0d, l = 5.0d;
Shape shape = new Sphere(r);
Console.WriteLine("球的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
shape = new Cylinder(r,h);
Console.WriteLine("圆柱的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
shape = new Cone(r,l);
Console.WriteLine("圆锥的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
Console.ReadKey();
}
}
}
第三种方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shangji1_03
{
public interface ICalAreaAndVolumn //声明接口
{
double getArea();
double getVolumn();
}
public class Sphere : ICalAreaAndVolumn //球类
{
private double radius;
public Sphere(double r) { radius = r; }
public double getArea()
{ return (4 * Math.PI * radius * radius); }
public double getVolumn()
{ return (4 * Math.PI * Math.Pow(radius, 3) / 3); }
}
public class Cylinder : ICalAreaAndVolumn //圆柱类
{
private double radius, height;
public Cylinder(double r, double h) { radius = r; height = h; }
public double getArea()
{ return (2 * Math.PI * radius * radius + 2 * Math.PI * radius * height); }
public double getVolumn()
{ return (Math.PI * radius * radius * height); }
}
public class Cone : ICalAreaAndVolumn //圆锥类
{
private double radius, height;
public Cone(double r, double h) { radius = r; height = h; }
public double getArea()
{
return (Math.PI * radius * (radius + Math.Sqrt(height * height + radius * radius)));
}
public double getVolumn()
{ return (Math.PI * radius * radius * height / 3); }
}
class Program
{
static void Main(string[] args)
{
Sphere sp = new Sphere(2);
Cylinder cd = new Cylinder(2, 10);
Cone cn = new Cone(2, 20);
ICalAreaAndVolumn[] array = { sp, cd, cn };
Console.WriteLine("球的表面积为:" + array[0].getArea().ToString() + ";体积为:" + array[0].getVolumn().ToString());
Console.WriteLine("圆柱的表面积为:" + array[1].getArea().ToString() + ";体积为:" + array[1].getVolumn().ToString());
Console.WriteLine("圆锥的表面积为:" + array[2].getArea().ToString() + ";体积为:" + array[2].getVolumn().ToString());
Console.ReadKey();
}
}
}
【注】接口的使用时的where & how,举例如下:
如何实现?