什么情况下它是有意义的使用"方法隐藏"? [英] What scenarios does it make sense to use "method hiding"?

查看:140
本文介绍了什么情况下它是有意义的使用"方法隐藏"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
方法隐藏在C#中使用有效的例子。为什么它是在框架中实现?什么是真正的全球优势。?






我想知道如何隐藏的方法可能在C#有用吗?如果你潜入这个概念,你可以找到方法隐藏的东西混淆并促使我们的面条代码 style.There有一些原因是:


1 - 如果你需要在你的派生类新方法,你可以定义一个新的方法,在派生类中的新名称。

2,在一个多态的方式使用,当你使用向上转型,预期的行为将无法实现,这可能会造成混淆。



(特别是这种奇怪的行为竟然如此明显,很明显,如果基类和派生类分别在不同的库,你想有他们的地方在代码中多态的用法)

  Base类
{
的public void方法(){Console.WriteLine(我是基类); }
}
类派生:基本
{
公开新void方法(){Console.WriteLine(我派生类);}
}
类节目
{
静态无效的主要(字串[] args)
{
基C =新派生的();
C.Method(); //输出:我是基地班
}
}



这样的一个行为可能会产生混淆,但如果我们用的虚拟的中的基本覆盖的中的导出(我的意思是多态的用法)的输出是:我在派生类。
和这个输出是什么,我想我们大多数人的预期。



换句话说,我认为,在一个层次,多数开发商预期在派生类中重写方法不就是想隐藏基类实现的新方法。



我无法理解为什么这么一个没有优点有坏事在C#语言?至少我想知道的是什么方法隐藏在C#中的最佳惯例?



感谢您的帮助。


解决方案

如果您在派生类中需要一种新的方法,你可以在派生类中定义一个新名称的新方法。




不总是。考虑在那里你推导出在一些其他图书馆暴露一个类,一个你不具有控制权的情况下。你一些方法添加到您的类。



之后,你所依赖的库的开发者增加了一些增强功能,并使用相同的名称为您添加到的方法之一的派生类。在这种情况下,隐藏仅是现有功能的保存。如果你改变你的方法名,你需要更新你的整个代码库,并打破你与谁在使用你的库人的兼容性。相反,你可以隐藏你现有的方法,并保持与你的家属,并与你在同一时间取决于库的兼容性。



个人的情况下我使用的方法隐藏最多的是实现对多个类克隆操作时;没有这个功能,我不能改变返回类型以匹配类的:

  A级
{
酒店的公共克隆()
{
返回CloneImpl();
}

受保护的虚拟一个CloneImpl()
{
返回MemberwiseClone();
}
}

B类:A
{
新的公众B的clone()
{
回报(B) CloneImpl();
}
}



受保护的虚拟方法意味着,无论哪个克隆()你打电话,你仍然得到正确类型的对象了。然而,参考你得到的将取决于该类型上引用您用来调用类型克隆()。这允许:

  B B =新的B(); $ B $条b B B2 = b.Clone(); //没得投,由于隐藏方法。 

A A = B;
一A2 = a.Clone(); //仍返回一个B的,只是类型为A.


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

解决方案

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.

这篇关于什么情况下它是有意义的使用"方法隐藏"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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