亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何調用base.base.method()?

如何調用base.base.method()?

C#
嗶嗶one 2019-12-27 09:21:32
// Cannot change source codeclass Base{    public virtual void Say()    {        Console.WriteLine("Called from Base.");    }}// Cannot change source codeclass Derived : Base{    public override void Say()    {        Console.WriteLine("Called from Derived.");        base.Say();    }}class SpecialDerived : Derived{    public override void Say()    {        Console.WriteLine("Called from Special Derived.");        base.Say();    }}class Program{    static void Main(string[] args)    {        SpecialDerived sd = new SpecialDerived();        sd.Say();    }}結果是:從特殊派生調用。從派生調用。/ *這不是預期的* /從Base調用。我如何重寫SpecialDerived類,以便不調用中間類“ Derived”的方法?更新: 我想從Derived而不是Base繼承的原因是Derived類包含許多其他實現。由于無法在base.base.method()此處執行操作,因此我認為最好的方法是執行以下操作?//無法更改源代碼class Derived : Base{    public override void Say()    {        CustomSay();        base.Say();    }    protected virtual void CustomSay()    {        Console.WriteLine("Called from Derived.");    }}class SpecialDerived : Derived{    /*    public override void Say()    {        Console.WriteLine("Called from Special Derived.");        base.Say();    }    */    protected override void CustomSay()    {        Console.WriteLine("Called from Special Derived.");    }}
查看完整描述

3 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

只想在此處添加此內容,因為即使經過很多時間,人們仍然會回到這個問題。當然,這是不好的做法,但原則上仍然有可能做作者想要做的事情:


class SpecialDerived : Derived

{

    public override void Say()

    {

        Console.WriteLine("Called from Special Derived.");

        var ptr = typeof(Base).GetMethod("Say").MethodHandle.GetFunctionPointer();            

        var baseSay = (Action)Activator.CreateInstance(typeof(Action), this, ptr);

        baseSay();            

    }

}


查看完整回答
反對 回復 2019-12-27
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

這是一種不良的編程習慣,在C#中是不允許的。這是不好的編程習慣,因為

  • grandbase的細節是base的實現細節;你不應該依賴他們?;愄峁┝薌randbase的抽象層。您應該使用該抽象,而不是構建旁路來避免這種抽象。

  • 您之所以從您的基礎派生出來,是因為您喜歡它的功能,并且想要重用和擴展它。如果您不喜歡它的功能,而是想要解決它而不是使用它,那么為什么您首先要從中得到呢?如果這是您要使用和擴展的功能,請自己從數據庫中派生。

  • 該庫可能出于安全性或語義一致性目的而需要某些不變式,這些不變式由該庫如何使用Grandbase方法的詳細信息維護。允許基類的派生類跳過維護那些不變式的代碼,可能會使基類處于不一致的損壞狀態。


查看完整回答
反對 回復 2019-12-27
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

答案(我知道這不是您要找的東西)是:


class SpecialDerived : Base

{

    public override void Say()

    {

        Console.WriteLine("Called from Special Derived.");

        base.Say();

    }

}

事實是,您只能與您繼承的類進行直接交互。將該類視為一個層-根據其派生類的需要,提供或多或少提供其父類的功能。


編輯:


您的編輯有效,但我想我會使用以下內容:


class Derived : Base

{

    protected bool _useBaseSay = false;


    public override void Say()

    {

        if(this._useBaseSay)

            base.Say();

        else

            Console.WriteLine("Called from Derived");

    }

}

當然,在實際的實現中,您可能會為擴展性和可維護性做更多類似的事情:


class Derived : Base

{

    protected enum Mode

    {

        Standard,

        BaseFunctionality,

        Verbose

        //etc

    }


    protected Mode Mode

    {

        get; set;

    }


    public override void Say()

    {

        if(this.Mode == Mode.BaseFunctionality)

            base.Say();

        else

            Console.WriteLine("Called from Derived");

    }

}

這樣,派生類就可以適當地控制其父母的狀態。


查看完整回答
反對 回復 2019-12-27
  • 3 回答
  • 0 關注
  • 529 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號