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

C#:任何方式跳过多基因中的一个基本调用?

发布时间:2020-12-15 06:27:10 所属栏目:百科 来源:网络整理
导读:class GrandParent{ public virtual void Foo() { ... }}class Parent : GrandParent{ public override void Foo() { base.Foo(); //Do additional work }}class Child : Parent{ public override void Foo() { //How to skip Parent.Foo and just get to th
class GrandParent
{
    public virtual void Foo() { ... }
}

class Parent : GrandParent
{
    public override void Foo()
    {
       base.Foo();

       //Do additional work
    }
}

class Child : Parent
{
    public override void Foo()
    {
        //How to skip Parent.Foo and just get to the GrandParent.Foo base?

        //Do additional work
    }
}

如上面的代码所示,我如何使Child.Foo()调用到GrandParent.Foo()而不是进入Parent.Foo()? base.Foo()首先将我带到Parent类.

解决方法

你的设计是错误的,如果你需要这个.
相反,将每类逻辑放在DoFoo中,不需要调用base.DoFoo.
class GrandParent
{
    public void Foo()
    {
        // base logic that should always run here:
        // ...

        this.DoFoo(); // call derived logic
    }

    protected virtual void DoFoo() { }
}

class Parent : GrandParent
{
    protected override void DoFoo()
    {    
       // Do additional work (no need to call base.DoFoo)
    }
}

class Child : Parent
{
    protected override void DoFoo()
    {  
        // Do additional work (no need to call base.DoFoo)
    }
}

(编辑:李大同)

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

    推荐文章
      热点阅读