我有以下场景:
- 接口(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 override 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();
}
}
关于c# - 如何扩展类并覆盖来自接口(interface)的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46867254/