衍生方法比覆盖在C#中强? [英] Derived method stronger than override in c#?

查看:108
本文介绍了衍生方法比覆盖在C#中强?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再烦人的问题... 的)

询问后的 前 - (这部分是有关我的问题) - 我得到一个答案:

  

查看C#4规范的§7.6.5.1:

     

的候选方法集被减少到包含从只方法   最派生类型:对于每一种方法的CF集合中,其中C是   其中该方法F被声明类型,在碱声明的所有方法   了C型从集合中删除。

确定。

我有这个code:// 使用.dump()就像一个的WriteLine命令

 公共类基地
    {
        公共无效美孚(串串){1使用.dump();}
        公共虚拟无效美孚(对象的字符串){2使用.dump();}
    }

    公共类儿童:基本
    {

        公众覆盖无效美孚(对象的字符串){4使用.dump();}
    }
 

但这code:

  Child C级=新的儿童();
c.Foo(D);
 

发出:1

但等待...

didnt我们说被减少到仅包含派生程度最大的类型,只有方法:

孩子有从父亲1功能公共无效美孚(串串)近控功能

那么为什么要选择它的基础的功能?确实继承的功能比覆盖更紧密?

时,它关系到一个事实,即覆盖在运行时?

帮助,请。

修改

你看这个情况?

 公共类基地
    {
        公共虚拟无效美孚(INT X){1使用.dump();}
    }

    公共类儿童:基本
    {
        公众覆盖无效美孚(INT X){2使用.dump();}
        公共无效美孚(对象x){3使用.dump();}
    }

无效的主要()
{
    Child C级=新的儿童();
    c.Foo(6); //发出3
}
 

解决方案

有两个概念,这里正被迷惑,超载和压倒一切的。超载是具有多个签名为同一名称的功能,选择一个叫的概念,基于该签名。重写是在派生类中重新定义一个方法的概念。

是否为子类推翻的方法定义一个不物质在所有这些功能将被调用,因为它不会改变任一方法的签名。根据定义,施工覆盖方法不能改变它的签名

因此​​,如果签名是不变的完全一样的机制来确定正确的函数调用基于签名会被用来对付既父和子类。

更新

事实上,有更多的给它一点点埃里克利珀指出,在<一个href="http://blogs.msdn.com/b/ericlippert/archive/2007/09/04/future-breaking-changes-part-three.aspx"相对=nofollow>他的博客。事实证明实际上,如果有一个,在它不会寻找在基类的任何方法的子类中的过载的匹配方法。其原因是理智的 - 避免重大更改 - 但是当你拥有两个基地和子类中的结果是有点不合逻辑

我也只能随声附和乔恩斯基特:鉴于这种奇怪,我的建议是,以避免过载跨越继承的边界......至少与方法:如果你扁平的层级有多种方法可以适用于特定的呼叫

(again annoying question...)

after asking this before - ( which is partly related to my question) - i got an answer :

See §7.6.5.1 of the C# 4 spec:

The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set.

ok.

i have this code : //.Dump() is like a WriteLine command...

 public class Base
    {
        public void Foo(string strings)  { "1".Dump();}
        public virtual void  Foo(object strings)  { "2".Dump();}
    }

    public class Child : Base
    {

        public override void  Foo(object strings)  { "4".Dump();}
    }

but this code :

Child c = new Child();
c.Foo("d");

emits : "1"

but wait ...

didnt we say that is reduced to contain only methods from the most derived types: ?

Child has 1 function from its father public void Foo(string strings) and a NEARER override function.

so why did he choose its base's function ? does the inherited function is CLOSER than the override ?

is it related to the fact that override is at runtime ?

help please.

edit

what about this situation ?

public class Base
    {    
        public virtual void  Foo(int x)  { "1".Dump();}
    }

    public class Child : Base
    {
        public override void  Foo(int x)  { "2".Dump();}
        public void Foo(object x) { "3".Dump();}    
    }

void Main()
{
    Child c = new Child();
    c.Foo(6); //emits "3"
}

解决方案

There are two concepts here which are being confused, overloading and overriding. Overloading is the concept of having multiple signatures for a function of the same name and choosing one to call based on that signature. Overriding is the concept of re-defining a method in a derived class.

Whether or not the child class overrode one of the method definitions does not matter at all to which function will be called because it does not change the signature of either method. By definition and construction overriding a method cannot change it's signature.

So, if the signature is unchanged exactly the same mechanics for determining the correct function to call based on the signature will be used against both the parent and child classes.

Update

In fact there is a little bit more to it as Eric Lippert points out in his blog. It turns out in fact that if there is a method that matches the overload in the child class it will not look for any methods in base classes. The reasons are sane - avoiding breaking changes - but the result is somewhat illogical when you own both the base and child class.

I can only echo Jon Skeet: "Given this oddness, my advice would be to avoid overloading across inheritance boundaries... at least with methods where more than one method could be applicable for a given call if you flattened the hierarchy"

这篇关于衍生方法比覆盖在C#中强?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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