簡體   English   中英

有沒有辦法在C#中調用super的super?

[英]Is there a way to call super's super in C#

我知道這是糟糕的設計,我只是懶得檢修代碼,我寧願走捷徑。

class Grandma
{
   public virtual void Mtd(){}
}

class Mommy : Grandma
{
   public override void Mtd(){ base.Mtd(); /* other stuff I wanna skip*/}
}

class Daughter : Mommy
{
   public override void Mtd(){ /*base.base.Mtd() //How can I do it ? */}
}

因為該方法是虛擬上傳將無法正常工作。 那么,這甚至可能嗎?

這是不可能的; 但是,如果你想要一個快捷方式,你可以在你的Mommy方法中添加一個類型檢查:

class Mommy : Grandma
{
    public override void Mtd()
    { 
        base.Mtd();   // calls `Grandma` implementation

        if (this.GetType() == typeof(Mommy))
        {
            // not executed for any derived class
        }

        if (!(this is Daughter))
        {
            // not executed for `Daughter`
        }
    }        
}

class Daughter : Mommy
{
    public override void Mtd()
    { 
        base.Mtd();   // calls `Mommy` implementation
    }
}

無法“跳過” Mommy覆蓋。 你必須以某種方式重構它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM