是否有过在那里派生类应隐藏的情况......? [英] Is there ever a situation where derived class should hide …?

查看:111
本文介绍了是否有过在那里派生类应隐藏的情况......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许一个愚蠢的问题,但假设基类A 定义虚拟方法V ,是有过的情况它将使一个派生C类隐藏 AV 通过声明一个新的虚拟感法CV 具有相同签名为 AV

 类节目
{
静态无效的主要(字串[] args)
{
A A =新C();
a.Print(); //输出这是B级

C C =新C();
c.Print(); //输出这是C级
}
}

等级A
{
公虚拟无效打印()
{
Console.WriteLine(这是一类);
}
}

B类:A
{
公共覆盖无效打印()
{
Console.WriteLine( 这是B级);
}
}

类C:B
{
公共虚拟无效打印()
{
Console.WriteLine( 这是C级);
}
}

感谢您


< DIV CLASS =h2_lin>解决方案

隐藏继承虚函数是不是应该做的是故意设计的一部分。语言支持虚拟藏身,使对象的框架更好地适应未来变化



例如:发行对象框架X不提供打印()函数1。 Bob决定在派生类自己的定义打印()函数来扩展几个框架10的对象。由于他计划覆盖它们在更具体的课,他也使print()函数虚的。



后来,发行对象框架的X 2被释放。 Bob决定提升他目前的项目中使用版本2,而不是1版瞒着鲍勃,对象框架10队也决定打印()将是一个有用的功能有,所以他们中的一个附加的虚拟打印()基类的框架。



通过虚拟隐藏,包含Bob的打印Bob的子类()实施应该编译并运行良好,即使不同的打印()现在存在于基类 - 即使有不同的方法签名。知道该基类打印()将继续使用它的代码,并且知道Bob的打印(代码)将继续使用它。切勿将二人见面。



如果没有虚拟躲藏,直到他做一些不平凡的手术,他的源代码,对消除名称冲突Bob的代码将不能编译打印()。有些人认为这是正确的事情(拒绝编译),但实际上需要修改现有的工作代码基础库中的任何转速都不会与客户去了好。他们会责怪框架,打破一切,说坏话吧。



这将是合理的鲍勃得到一个编译器警告有关基本打印由Bob的打印被遮蔽,但这不是一个致命的错误。这件事情,鲍勃或许应该清理(通过重命名或消除他的print()函数)尽快以避免人为的混乱。


Perhaps a stupid question, but assuming base class A defines a virtual method V, is there ever a situation where it would make sense for a derived class C to hide A.V by declaring a new virtual method C.V with the same signature as A.V:

class Program
{
    static void Main(string[] args)
    {
        A a = new C();
        a.Print(); // prints "this is in B class"

        C c = new C();
        c.Print();// prints "this is in C class"
    }
}

class A
{
    public virtual void Print()
    {
        Console.WriteLine("this is in A class");
    }
}

class B:A
{
    public override void Print()
    {
        Console.WriteLine("this is in B class");
    }
}

class C : B
{
    public virtual void Print()
    {
        Console.WriteLine("this is in C class");
    }
}

Thank you

解决方案

Hiding inherited virtuals is not something that should be done as part of a deliberate design. Languages support virtual hiding to make object frameworks more resilient to future change.

Example: Release 1 of object framework X does not provide a Print() function. Bob decides to extend a few of the framework X objects by defining Print() functions in descendant classes of his own. Since he plans to override them in more specific classes, he also makes the Print() function virtual.

Later, Release 2 of object framework X is released. Bob decides to upgrade his current project to use Release 2 instead of Release 1. Unbeknownst to Bob, the object framework X team also decided Print() would be a useful function to have and so they added a virtual Print() in one of the base classes of the framework.

With virtual hiding, Bob's descendant classes containing Bob's Print() implementation should compile and run fine even though a different Print() now exists in the base classes - even with a different method signature. The code that knows about the base class Print() will continue to use it, and the code that knows about Bob's Print() will continue to use it. Never the two shall meet.

Without virtual hiding, Bob's code will not compile at all until he does some non-trivial surgery to his source code to eliminate the name conflict on Print(). Some would argue this is the "correct" thing to do (refuse to compile) but realistically any rev of a base library that requires revising existing working code will not go over well with customers. They will blame the framework for breaking everything and speak ill of it.

It would be reasonable for Bob to get a compiler warning about the base Print being obscured by Bob's print, but this isn't a fatal error. It's something that Bob should probably clean up (by renaming or eliminating his Print() function) as soon as possible to avoid human confusion.

这篇关于是否有过在那里派生类应隐藏的情况......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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