簡體   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