繁体   English   中英

如何扩展类并重写来自接口的方法?

[英]How to extend a class and override a method which is from interface?

我有以下情况:

  • 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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM