【发布时间】:2017-10-21 19:34:37
【问题描述】:
我有以下场景:
- 接口
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方法标记为virtual以便能够在派生类中覆盖它
标签: c# oop inheritance interface