如何扩展一个类并覆盖一个来自接口的方法?

San*_*osh 1 c# oop inheritance interface

我有以下场景:

  • 接口IShape定义方法Draw.
  • Circle实现IShape和方法Draw.
  • Rectangle实现IShape和方法Draw.
  • class 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

有帮助吗?

sel*_*ami 5

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();
    }
}
Run Code Online (Sandbox Code Playgroud)