简体   繁体   English

方法覆盖和可选参数

[英]Method Overriding and Optional Parameters

Would someone care to explain how this code produces the folowing output? 有人愿意解释一下此代码如何产生以下输出吗?

using System;

namespace ConsoleApplication1
{
    class Test
    {
        public override string ToString() { return "ToString override"; }
        public string ToString(string optional = "")
          { return String.Format("ToString with optional parameter {0}", optional); }
    }

    class Test2
    {
        public new string ToString() { return "ToString new"; }
        public string ToString(string optional = "")
          { return String.Format("ToString with optional parameter {0}", optional); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test one = new Test();
            Test2 two = new Test2();
            Console.WriteLine(one);
            Console.WriteLine(one.ToString());
            Console.WriteLine(one.ToString("foo"));
            Console.WriteLine("--");
            Console.WriteLine(two);
            Console.WriteLine(two.ToString());
            Console.WriteLine(two.ToString("bar"));
            Console.ReadKey();
        }
    }
}

ToString override ToString覆盖

ToString with optional parameter 具有可选参数的ToString

ToString with optional parameter foo 具有可选参数foo的ToString

-- -

ConsoleApplication1.Test2 ConsoleApplication1.Test2

ToString new ToString新

ToString with optional parameter bar 带有可选参数栏的ToString

Okay, as there's general interest, here's a quick version: 好的,因为大家都很感兴趣,这里有一个快速版本:

Console.WriteLine(one) Console.WriteLine(之一)

This will use the WriteLine(object) overload, which will in turn execute the object.ToString() virtual method, overridden in One - hence the output of ToString override 这将使用WriteLine(object)重载,这将依次执行object.ToString()虚拟方法,该方法在One被覆盖-因此ToString override的输出将被ToString override

Console.WriteLine(one.ToString()) Console.WriteLine(one.ToString())

This will look at One and see which methods have newly declared methods - discounting overrides. 这将查看One并查看哪些方法具有新声明的方法 -折扣覆盖。 There's exactly one such method which is applicable - the one with the optional parameter. 恰好有一种这样的方法适用-一种带有可选参数的方法。 So that gets executed, using the default value, leading to output of ToString with optional parameter . 这样就可以使用默认值执行该操作,并使用ToString with optional parameter导致ToString with optional parameter输出。

Console.WriteLine(one.ToString("foo")) Console.WriteLine(one.ToString( “富”))

Same again, but this time the compiler doesn't need to use the default value, hence ToString with optional parameter foo 再次相同,但是这次编译器不需要使用默认值,因此ToString with optional parameter foo

Console.WriteLine(two) Console.WriteLine(二)

Again, this will call the virtual object.ToString() method from WriteLine(object) . 同样,这将从WriteLine(object)调用虚拟object.ToString()方法。 The method hasn't been overridden, so the default implementation returning the name of the type is used, leading to output of ConsoleApplication1.Test2 . 该方法尚未被重写,因此使用返回类型名称的默认实现,从而导致ConsoleApplication1.Test2输出。

Console.WriteLine(two.ToString()) Console.WriteLine(two.ToString())

The compiler looks at all the method declared in Two which aren't overriding virtual methods. 编译器查看Two中声明的所有方法,这些方法不会覆盖虚拟方法。 In this case, there are two such methods - the parameterless one and the one with the optional parameter. 在这种情况下,有两种方法-无参数方法和一种带有可选参数的方法。 The parameterless one is included because it's new rather than overriding a base class method. 包含无参数方法是因为它是新方法,而不是覆盖基类方法。

The parameterless method is deemed a "better" candidate because the compiler prefers to call a method which doesn't need any optional parameters filling in. Hence the output is ToString new 无参数方法被认为是“更好”的候选方法,因为编译器更喜欢调用不需要填写任何可选参数的方法。因此,输出为ToString new

Console.WriteLine(two.ToString("bar")) Console.WriteLine(two.ToString( “酒吧”))

Again, the compiler looks at all the method declared in Two which aren't overriding virtual methods. 再次,编译器查看在Two中声明的所有方法,这些方法不会覆盖虚拟方法。 In this case, there are two such methods - but the parameterless one isn't applicable, leaving just the one with the optional parameter. 在这种情况下,有两种这样的方法-但无参数的方法不适用,只剩下一种带有可选参数的方法。 The compiler doesn't need to use the default value of the optional parameter here, as it's got an argument anyway... so the output is ToString with optional parameter bar 编译器不需要在这里使用可选参数的默认值,因为它仍然有一个参数...因此输出是ToString with optional parameter bar

For much more on this, read the C# language specification - or for a half-way house, see my article on overloading . 有关此内容的更多信息,请阅读C#语言规范-或中途学习,请参阅我有关重载的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM