用c#定义一个抽象类Shape,由它派生出两个子类:长方形rectangle和正方形类square,

用c#定义一个抽象类Shape,由它派生出两个子类:长方形rectangle和正方形类square,通过抽象方法的实现来计算两种图形的周长和面积,要求程序能够体现对象的多态性。
哪位大神知道这么弄,急啊

public abstract class Shape
{
    public virtual double ComputeCircumference()
    {
        return 0;
    }
    
    public virtual double ComputeArea()
    {
        return 0;
    }
}

public class Rectangle : Shape
{
    public double Width
    {
        get;
        set;
    }
    
    public double Height
    {
        get;
        set;
    }
    
    public override double ComputeCircumference()
    {
        return (this.Width + this.Height) * 2;
    }
    
    public virtual double ComputeArea()
    {
        return this.Width * this.Height;
    }
}

public class Square : Shape
{
    public double Width
    {
        get;
        set;
    }
    
    public override double ComputeCircumference()
    {
        return this.Width * 4;
    }
    
    public virtual double ComputeArea()
    {
        return this.Width * this.Width;
    }
}

温馨提示:答案为网友推荐,仅供参考
相似回答