Can a method be abstract but not virtual?
据我所知,被标记为抽象的方法是隐式虚拟的。原因:假设给定对象的编译时类型是抽象的。如果正在调用对象的一个抽象方法,则要执行的实际方法是在对象的运行时类型中定义的方法。不是吗?如果我是对的,那么抽象方法的行为就好像它也是虚拟的。
尽管如此,我还是成功地同时标记了抽象和虚拟的C方法:
1
| public abstract virtual void crazy(); |
我想这意味着一个抽象方法不一定是虚拟的,抽象实际上是与虚拟正交的。
我怎么了?抽象方法怎么可能不是虚拟的?
- 字母名称
- @romoku every EDOCX1 plographic 1.Wi method is virtual,you just can't add the EDOCX1 plographic 2.So I don't consider that sentence wrong.
- Perhaps you meant the class is abstract but the method is virtual?
- an abstract method is a virtual method without implementation.
- @Codeschaos:I think the ambiguty stems from the word"declared"-is it declaring by explicitly writing the keyword,or declaring as in declaring the method in general?or,is it declaring a method that is both abstract and virtual,or declaring a given method as such?
- @编码A method cannot be declared EDOCX1
- 5.Instead of using"declared"use"marked".That should be a bit more obvious as to what is meant and is in line with the wording of the compiler.
不能将方法标记为abstract和virtual。它将导致编译器错误:
The abstract method 'Namespace.Class.Foo()' cannot be marked virtual
剩下的问题是正确的:abstract方法是隐式virtual方法。
来自MSDN
- 抽象方法是隐式的虚拟方法。
- 抽象方法声明只允许在抽象类中使用。
- 因为抽象方法声明不提供实际的实现,所以没有方法体;方法声明只以分号结尾,签名后面没有大括号()。
- 实现由重写方法提供,该方法是非抽象类的成员。
- 在抽象方法声明中使用静态或虚拟修饰符是错误的。
- The implementation is provided by an overriding method, which is a member of a non-abstract class.—事实并非如此。抽象方法可以在抽象类中重写。例如:public abstract class A { public abstract void Foo(); } public abstract class B : A { public override void Foo() { Console.WriteLine("B.Foo()"); } }。
不能将虚拟修饰符与静态、抽象、私有或重写修饰符一起使用。
- http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.100).aspx