简体   繁体   English

用虚拟方法覆盖抽象方法

[英]Overriding an abstract method with a virtual one

I'm trying to override an abstract method in an abstract class with a virtual method in a child class. 我试图在子类中使用虚方法覆盖抽象类中的抽象方法。 I (assumed until now?) understand the difference between abstract and virtual methods. 我(假设到现在?)了解抽象方法和虚方法之间的区别。

Obviously I'm not able to do it but my question is... why? 显然我无法做到,但我的问题是......为什么? Based on the accepted answer here and the following scenario, I just don't see the problem: 基于此处接受的答案和以下场景,我只是没有看到问题:

    public abstract class TopLevelParent
    {
        protected abstract void TheAbstractMethod();
    }

    public class FirstLevelChild1 : TopLevelParent
    {
        protected override void TheAbstractMethod()
        {

        }
    }

    public class FirstLevelChild2 : TopLevelParent
    {
        protected virtual override void TheAbstractMethod()
        {
            //Do some stuff here
        }
    }

    public class SecondLevelChild : FirstLevelChild2
    {
        //Don't need to re-implement the method here... my parent does it the way I need.
    }

So obviosuly what I've done is have a top-level parent with two inheriting children and another class inheriting from one of those. 所以,我所做的就是让一个顶级父级有两个继承子级,另一个继承自其中一个继承子级。 Again, based on the accepted answer in the link I posted above: 同样,根据我在上面发布的链接中接受的答案:

"A virtual function, is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality." “一个虚函数,基本上就是说看,这里的功能对于子类来说可能是也可能不够好。所以如果它足够好,使用这个方法,如果没有,那么覆盖我,并提供你自己的功能。 “

and that the second level child will inherit the virtual method from its parent, thus satisfying the implementation requirement of the abstract method from its top-most parent... what's the problem? 并且第二级子级将从其父级继承虚拟方法,从而满足其最顶层父级的抽象方法的实现要求......问题是什么?

I'm missing some detail somewhere that's hindering my understanding of this... 我错过了某些阻碍我对此理解的细节......

An override method is implicitly virtual (in the sense that it can be overridden in a subclass), unless marked as sealed . override方法隐式虚拟的(在某种意义上它可以在子类中重写),除非标记为sealed

Observe: 注意:

public class FirstLevelChild1 : TopLevelParent
{
    protected override void TheAbstractMethod() { }
}

public class SecondLevelChild1 : FirstLevelChild1
{
    protected override void TheAbstractMethod() { } // No problem
}

public class FirstLevelChild2 : TopLevelParent
{
    protected sealed override void TheAbstractMethod() { }
}

public class SecondLevelChild : FirstLevelChild2
{
    protected override void TheAbstractMethod() { } 
    // Error: cannot override inherited member 
    // 'FirstLevelChild2.TheAbstractMethod()' because it is sealed
}

An abstract method is already virtual all the way down the inheritance chain - there's no need to declare it virtual in a subclass to allow the subclass to override it - the subclass can already override it. 一个abstract方法已经virtual沿着继承链中的所有的方式-没有必要宣布其virtual的子类,允许子类覆盖它-子类已经可以覆盖它。

If you don't provide an implementation, the closest implementation (looking down the inheritance list) will be used. 如果您提供实现,将使用最接近的实现(向下查看继承列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM