110,502
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
//调用函数(步骤5,6)输出
SpitShapeChildren();
Console.ReadKey();
}
//步骤5
public static shape[] StoreShape()
{
return new List<shape>() {
new tangle(10d),new circle(10d),new rectangle(10d),
}.ToArray();
}
//步骤6
public static void SpitShapeChildren()
{
shape[] shapes = StoreShape();
if (shapes != null && shapes.Length > 0)
{
foreach (shape item in shapes)
{
//输出面积
Console.Write(item.mianji() + Environment.NewLine);
//输出周长
Console.Write(item.zhouchang() + Environment.NewLine);
//输出类型
Console.Write(item.mianji().GetType() + Environment.NewLine);
}
}
}
}
#region MyRegion
//步骤1
public abstract class shape
{
public shape()
{
}
public shape(double x, double y)
{
this.x = x;
this.y = y;
}
public double x { get; set; }
public double y { get; set; }
/// <summary>
/// 抽象方法求面积
/// </summary>
/// <returns></returns>
public abstract double mianji();
/// <summary>
/// 虚方法求周长
/// </summary>
/// <returns></returns>
public virtual double zhouchang()
{
return x+y;
}
}
//步骤2
public class tangle : shape
{
public tangle(double z) : base(10, 10)
{
this.z = z;
}
double z { get; set; }
public sealed override double mianji()
{
return (this.z + base.x + base.y) / 2;
}
public override double zhouchang()
{
return (this.z + base.x + base.y);
}
}
//步骤3
public class circle : shape
{
public circle(double R) : base(R / 2, 0)
{
this.R = R;
}
const double pi = 3.14d;
double R { get; set; }
public override double mianji()
{
return Math.Pow(base.x, 2) * pi;
}
public override double zhouchang()
{
return base.x * 2 * pi;
}
}
//步骤4
public class rectangle : shape
{
public rectangle(double z) : base(10, 10)
{
this.z = z;
}
double width { get => base.x; set => width = base.x; }
double height { get => base.y; set => width = base.y; }
double z { get; set; }
public override double mianji()
{
return (base.x + base.y + this.z) / 2;
}
}
#endregion