在C#中方法重载的不同的行为 [英] Different behaviour of method overloading in C#

查看:117
本文介绍了在C#中方法重载的不同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我所经历的C#脑筋急转弯( HTTP://www.yoda.arachsys .COM / CSHARP / teasers.html )和跨一个问题就来了:应该是什么这个代码的输出


I was going through C# Brainteasers (http://www.yoda.arachsys.com/csharp/teasers.html) and came across one question: what should be the output of this code?

class Base
{
    public virtual void Foo(int x)
    {
        Console.WriteLine ("Base.Foo(int)");
    }
}

class Derived : Base
{
    public override void Foo(int x)
    {
        Console.WriteLine ("Derived.Foo(int)");
    }

    public void Foo(object o)
    {
        Console.WriteLine ("Derived.Foo(object)");
    }
}

class Test
{
    static void Main()
    {
        Derived d = new Derived();
        int i = 10;
        d.Foo(i);  // it prints ("Derived.Foo(object)"
    }
} 

但是,如果我更改代码以

But if I change the code to

class Derived 
{
    public void Foo(int x)
    {
        Console.WriteLine("Derived.Foo(int)");
    }

    public void Foo(object o)
    {
        Console.WriteLine("Derived.Foo(object)");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        int i = 10;
        d.Foo(i); // prints  Derived.Foo(int)");

        Console.ReadKey();
    }
}

我想为什么当我们在继承VS不继承输出得到改变;为什么是方法重载在这两种情况下表现不同。

I want to why the output is getting changed when we are inheriting vs not inheriting; why is method overloading behaving differently in these two cases?

推荐答案

我在href=\"http://www.yoda.arachsys.com/csharp/teasers-answers.html\">回答页面的

As I specified in the answers page:

Derived.Foo(对象)印刷 - 选择一个过载时,如果有在派生类声明的任何兼容
的方法,在基类中声明的所有签名都被忽略
! - 即使他们在同一个派生类中重写是

Derived.Foo(object) is printed - when choosing an overload, if there are any compatible methods declared in a derived class, all signatures declared in the base class are ignored - even if they're overridden in the same derived class!

在换句话说,编译器会在最派生类(基于表达式的编译时类型),这些新鲜声明,并认为如果有任何适用的方法。如果是这样,它使用最好的一个可用。如果没有是适用的,它试图基类,等等。覆盖方法不能算作在派生类中被宣布。

In other words, the compiler looks at methods which are freshly-declared in the most derived class (based on the compile-time type of the expression) and sees if any are applicable. If they are, it uses the "best" one available. If none is applicable, it tries the base class, and so on. An overridden method doesn't count as being declared in the derived class.

请参阅7.4.3节和C#3规范的更详细的信息7.5.5.1。

See sections 7.4.3 and 7.5.5.1 of the C# 3 spec for more details.

现在对于究竟为什么这就像指定的 - 我不知道。这是有道理的,我认为在派生类中声明的方法优先于那些在基类中声明,否则你碰到的脆基类的问题 - 在基类中添加一个方法可以采用改变代码的含义派生类。但是,如果派生类的覆盖的基类中声明的方法,它显然意识到这一点,所以脆性该元素并不适用。

Now as for exactly why it's specified like that - I don't know. It makes sense to me that methods declared in the derived class take precedence over those declared in the base class, as otherwise you run into the "brittle base class" problem - adding a method in the base class could change the meaning of code using the derived class. However, if the derived class is overriding the method declared in the base class, it's clearly aware of it, so that element of brittleness doesn't apply.

这篇关于在C#中方法重载的不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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