问题描述
显然你不能将 virtual
修饰符与 override
修饰符一起使用.
So apparently you cannot use the virtual
modifier with the override
modifier.
virtual
- 可以被覆盖的方法
virtual
- a method that can be overridden
override
- 覆盖其父类中同名方法的方法
override
- a method that is overriding a method of the same name in its parent's class
这让我相信,如果我在子类中重写了一个方法,如果那个孩子有一个孩子,你就不能再重写那个方法了.
This leads me to believe that if I override a method in a child class, if that child has a child you can't override that method again.
可以肯定地说,如果您将 override
和 virtual
放在方法声明中,您将在 C# 中得到编译错误.
And it is safe to say that if you put override
and virtual
in a method declaration you will get a compile error in C#.
但是我不明白为什么我在下面编写的代码会以它的方式工作
However I can't understand why the code I made below works the way in which it does
using System;
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("Drawing Object");
}
}
public class DrawDemo
{
public static int Main()
{
DrawingObject[] dObj = new DrawingObject[3];
dObj[0] = new DrawingObject();
dObj[1] = new Line();
dObj[2] = new LittleLine();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
Console.Read();
return 0;
}
}
public class Line : DrawingObject
{
public override void Draw()
{// the method above me is in fact virtual because LittleLine overid it?
Console.WriteLine("I'm a Line.");
}
}
public class LittleLine : Line
{
public override void Draw()
{
Console.WriteLine("I'm a Little Line.");
}
}
这是输出:
绘图对象
我是一条线.
我是一条小线.
所以 Line
中的 draw 方法看起来好像被 LittleLine
覆盖了.这段代码实际上并没有覆盖它,还是编译器做了其他一些技巧?还是我不了解 virtual
和 override
的上下文?
So the draw method in Line
looks as though it was overridden by LittleLine
. Is this code not actually overriding it, or is the compiler doing some other trick? Or am I not understanding the context of virtual
and override
?
推荐答案
你可以将某个方法声明为virtual
一次,但你可以override
多次如您所愿-覆盖不是最终的,并且它不限制从第一个覆盖类继承的类.最终将执行的方法是最后一个覆盖虚拟方法的方法.因此,您的代码确实按预期运行.
You can declare a certain method as virtual
only once, but you can override
it as many times as you want - an override is not final, and it does not limit classes that inherit from the first overriding class. The method that will eventually execute is the last one the overrides the virtual method. So your code does behave as expected.
C# 在覆盖方面非常冗长——你有比 C++ 或 Java 更多的说明符.让程序员指定确切的意图是这样的:
C# is very verbose with regard to overriding - you have more specifiers than C++ or Java. It is so to let the programmer specify the exact intent:
- 你使用
virtual
来指定一个方法可以被子类覆盖. - 您使用
override
来指定您正在重写一个您知道是虚拟的方法(如果不是,编译器将报告错误). - 您使用
sealed
来防止进一步覆盖. - 您使用
new
隐藏而不是覆盖.
- You use
virtual
to specify a method can be overridden by subclasses. - You use
override
to specify you are overriding a method that you know is virtual (if it's not, the compiler will report an error). - You use
sealed
to prevent further overriding. - And you use
new
to hide instead of override.
这可能会令人困惑,有时甚至很烦人,但它可以确保您真正知道自己在做什么,并且可以让您的意图自我记录.
This can be confusing and sometimes annoying, but it ensures you actually know what you're doing, and it makes your intention self-documented.
这篇关于不能在 C# 中的同一方法上使用虚拟和覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!