What scenarios does it make sense to use “method hiding”? [duplicate]

戏子无情 提交于 2019-11-30 03:10:13

问题


Possible Duplicate:
method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.?

I am wondering how method hiding could be useful in c# ? If you dive into the concept ,you can find that method hiding is something confusing and drives us to spaghetti code style.There are some reasons for that :

1- If you need a fresh method in your Derived class you can define a new method with a new name in your derived class.

2-In a polymorphic manner usage when you use upcasting, the expected behavior would not be achieved and this could be confusing.

(Specially this strange behavior could be so apparent and obvious if the Base class and the Derived Class were in the different libraries and you want to have a polymorphic usage of them in somewhere in your code)

class Base
{  
    public void Method() { Console.WriteLine("I am Base Class"); }
}
class Derived: Base
{
    public new void Method() { Console.WriteLine("I am Derived Class");}
}
class Program
{
    static void Main(string[] args)
    {
        Base C = new Derived();
        C.Method(); //output:"I am Base Class"
    }
}

Such a behavior could be confusing, but If we used "virtual" in "Base" and "override" in "Derived" (I mean a polymorphic usage) the output was: "I am Derived Class". and this output is what that I think the most of us expected.

In other words I believe that in a hierarchy, most of developers expected for overridden methods in a derived class Not New methods that wanna hide the base class implementation.

I cant understand why such a bad thing that has no advantages there is in c# language? at least I want to know what is the best usages of method hiding in c# ?

thank you for your help


回答1:


If you need a fresh method in your Derived class you can define a new method with a new name in your derived class

Not always. Consider the case where you derive a class that is exposed in some other library, one that you do not have control over. You add some methods to your class.

Later, the developer of the library you depend on adds some enhancements and uses the same name as one of the methods you added to your derived class. Hiding in this case is merely the preservation of existing functionality. If you change your method name, you have to update your entire code base, and you break compatibility with anyone who is using your library. Instead, you can hide your existing method and retain compatibility with your dependents and with the library you depend on at the same time.

Personally, the case where I use method hiding the most is when implementing a clone operation on multiple classes; without this feature, I could not change the return type to match that of the class:

class A
{
    public A Clone()
    {
        return CloneImpl();
    }

    protected virtual A CloneImpl()
    {
        return MemberwiseClone();
    }
}

class B : A
{
    new public B Clone()
    {
        return (B)CloneImpl();
    }
}

The protected virtual method means that no matter which Clone() you call, you are still getting the correct type of object back. However, the type of reference you get will depend on which reference type you used to invoke Clone(). This allows:

B b = new B();
B b2 = b.Clone(); // Did not have to cast, thanks to the hiding method.

A a = b;
A a2 = a.Clone(); // Still returns a B, only typed as an A.


来源:https://stackoverflow.com/questions/14461725/what-scenarios-does-it-make-sense-to-use-method-hiding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!