加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如果虚拟方法被声明为abstract

发布时间:2020-12-15 04:26:21 所属栏目:百科 来源:网络整理
导读:我的朋友问我抽象方法是否可以有虚拟修饰符.我说,不. 因为抽象方法也隐式也是虚方法,所以它不能具有虚拟修饰符. 但在阅读one of the MSDN articles时,我看到了这个: … If a virtual method is declared abstract ,it is still virtual to any class inheri
我的朋友问我抽象方法是否可以有虚拟修饰符.我说,不.
因为抽象方法也隐式也是虚方法,所以它不能具有虚拟修饰符.

但在阅读one of the MSDN articles时,我看到了这个:


If a virtual method is declared abstract,it is still virtual to any
class inheriting from the abstract class. A class inheriting an
abstract method cannot access the original implementation of the
method—in the previous example,DoWork on class F cannot call DoWork
on class D. In this way,an abstract class can force derived classes
to provide new method implementations for virtual methods….

我无法正确理解第一句话.你能不能解释一下他们想说什么?

谢谢.

解决方法

当您查看引用段落正上方的代码示例时,它会变得更清晰:
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

虚拟方法D.DoWork由E继承,并且在那里声明为abstract.该方法仍然是虚拟的,它也刚刚变得抽象.

正确地说,抽象方法总是虚拟的.如果你的朋友仍然不相信,那么这是一个official quote:

An abstract method is implicitly a virtual method.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读