简体   繁体   中英

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

I have the following scenario:

  • interface IShape defines method Draw .
  • class Circle implements IShape and the method Draw .
  • class Rectangle implements IShape and the method Draw .
  • class Square extends Rectangle and overrides the method Draw .

I wrote the code as following for the above scenario:

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

I am unable to get the last scenario which is class Square extends Rectangle and overrides the method Draw .

Any help?

Rectangle.Draw virtual, Square.Draw override

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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