我有以下情况:


接口IShape定义方法Draw
Circle实现IShape和方法Draw
Rectangle实现IShape和方法Draw
Square扩展Rectangle并覆盖方法Draw


对于上述情况,我编写了以下代码:

class Program
{
    static void Main(string[] args) { }
}

public interface IShape
{
    void Draw();
}

public class Circle : IShape
{
    public void Draw()
    {
        throw new NotImplementedException();
    }
}

public class Rectangle : IShape
{
    public void Draw()
    {
        throw new NotImplementedException();
    }
}

public class Square : Rectangle
{
    public virtual void Draw()
    {
        throw new NotImplementedException();
    }
}


我无法获得最后一个场景class Square extends Rectangle and overrides the method Draw

有什么帮助吗?

最佳答案

虚拟Rectangle.Draw,Square.Draw覆盖

public class Rectangle : IShape
{
    public virtual void Draw()
    {
        throw new NotImplementedException();
    }
}

public class Square : Rectangle
{
    public override void Draw()
    {
        throw new NotImplementedException();
    }
}

关于c# - 如何扩展类并重写来自接口(interface)的方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46867254/

10-13 02:15