[英]How to extend a class and override a method which is from interface?
I have the following scenario: 我有以下情况:
IShape
defines method Draw
. IShape
接口定义方法Draw
。 Circle
implements IShape
and the method Draw
. Circle
类实现IShape
和方法Draw
。 Rectangle
implements IShape
and the method Draw
. Rectangle
类实现IShape
和方法Draw
。 Square
extends Rectangle
and overrides the method Draw
. Square
类扩展Rectangle
并重写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
. 我无法获得最后一个场景,该场景是
class Square extends Rectangle and overrides the method Draw
。
Any help? 有什么帮助吗?
Rectangle.Draw virtual, Square.Draw override 虚拟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.