如何调用base.base.method()? [英] How to call base.base.method()?

查看:158
本文介绍了如何调用base.base.method()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //无法更改源$ C ​​$ C
Base类
{
    公共虚拟无效说()
    {
        Console.WriteLine(从基地调用。);
    }
}//无法更改源$ C ​​$ C
派生类:基础
{
    公共覆盖无效说()
    {
        Console.WriteLine(叫源于);
        base.Say();
    }
}类SpecialDerived:派生
{
    公共覆盖无效说()
    {
        Console.WriteLine(特种称为派生。);
        base.Say();
    }
}类节目
{
    静态无效的主要(字串[] args)
    {
        SpecialDerived SD =新SpecialDerived();
        sd.Say();
    }
}

的结果是:

 特种派生调用。
所谓的衍生。 / *这不是预期* /
从基本调用。

我怎么可以重写SpecialDerived类,以便中产阶级派生的方法,不叫?

更新:
为什么我想从派生,而不是基地派生类继承的原因包含了很多其他的实现。因为我不能做 base.base.method()在这里,我想最好的办法是做下面的?

<击> //无法更改源$ C ​​$ C

 类派生:基地
{
    公共覆盖无效说()
    {
        CustomSay();        base.Say();
    }    受保护的虚拟无效CustomSay()
    {
        Console.WriteLine(叫源于);
    }
}类SpecialDerived:派生
{
    / *
    公共覆盖无效说()
    {
        Console.WriteLine(特种称为派生。);
        base.Say();
    }
    * /    保护覆盖无效CustomSay()
    {
        Console.WriteLine(特种称为派生。);
    }
}


解决方案

这是一个不好的编程习惯,并在C#中不允许的。这是一个不好的编程习惯,因为


  • grandbase的细节是碱的实现细节;你不应该依靠它们。该基类提供了上层建筑的grandbase一个抽象的;你应该使用抽象的,而不是建立一个绕开它。


  • 您从您碱的,因为你喜欢它做什么,想重用和扩展它。如果你不喜欢它做什么,想解决它,而不是使用它,那你为什么从中获得摆在首位?从grandbase派生自己,如果这就是你想要使用和扩展功能。


  • 的碱可能需要对于由的基础如何使用grandbase的方法的细节保持安全或语义一致性目的某些变量。允许派生类基地跳过code维持不变者可以把基地变成不一致,损坏状态。


// Cannot change source code
class Base
{
    public virtual void Say()
    {
        Console.WriteLine("Called from Base.");
    }
}

// Cannot change source code
class 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();
    }
}

The result is:

Called from Special Derived.
Called from Derived. /* this is not expected */
Called from Base.

How can I rewrite SpecialDerived class so that middle class "Derived"'s method is not called?

UPDATE: The reason why I want to inherit from Derived instead of Base is Derived class contains a lot of other implementations. Since I can't do base.base.method() here, I guess the best way is to do the following?

// Cannot change source code

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.");
    }
}

解决方案

This is a bad programming practice, and not allowed in C#. It's a bad programming practice because

  • The details of the grandbase are implementation details of the base; you shouldn't be relying on them. The base class is providing an abstraction overtop of the grandbase; you should be using that abstraction, not building a bypass to avoid it.

  • You derived from your base because you like what it does and want to reuse and extend it. If you don't like what it does and want to work around it rather than work with it, then why did you derive from it in the first place? Derive from the grandbase yourself if that's the functionality you want to use and extend.

  • The base might require certain invariants for security or semantic consistency purposes that are maintained by the details of how the base uses the methods of the grandbase. Allowing a derived class of the base to skip the code that maintains those invariants could put the base into an inconsistent, corrupted state.

这篇关于如何调用base.base.method()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆