多态与多重分派有什么区别? [英] What's the difference between Polymorphism and Multiple Dispatch?

查看:144
本文介绍了多态与多重分派有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...还是同一回事?我注意到每个人都有自己的Wikipedia条目:多态

...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism, Multiple Dispatch, but I'm having trouble seeing how the concepts differ.

编辑:超载如何适应所有这一切? ?

And how does Overloading fit into all this?

推荐答案

多态性是一种工具,它允许语言/程序在运行时根据发送给该方法的参数类型来决定要调用的方法.

Polymorphism is the facility that allows a language/program to make decisions during runtime on which method to invoke based on the types of the parameters sent to that method.

语言/运行时使用的参数数量决定了语言支持的多态性的类型".

The number of parameters used by the language/runtime determines the 'type' of polymorphism supported by a language.

单派发是一种多态性,其中仅使用一个参数(消息的接收方-thisself)来确定呼叫.

Single dispatch is a type of polymorphism where only one parameter is used (the receiver of the message - this, or self) to determine the call.

多重调度是一种多态性,其中多个参数用于确定要调用的方法.在这种情况下,将使用接收方以及方法参数的类型来告知要调用的方法.

Multiple dispatch is a type of polymorphism where in multiple parameters are used in determining which method to call. In this case, the reciever as well as the types of the method parameters are used to tell which method to invoke.

因此,您可以说多态是通用术语,而多调度和单调度是多态的特定类型.

So you can say that polymorphism is the general term and multiple and single dispatch are specific types of polymorphism.

附录:重载发生在编译时.它使用编译期间可用的类型信息来确定要调用的方法类型.单次/多次分派在运行时发生.

Addendum: Overloading happens during compile time. It uses the type information available during compilation to determine which type of method to call. Single/multiple dispatch happens during runtime.

示例代码:

using NUnit.Framework;

namespace SanityCheck.UnitTests.StackOverflow
{
    [TestFixture]
    public class DispatchTypes
    {
        [Test]
        public void Polymorphism()
        {
            Baz baz = new Baz();
            Foo foo = new Foo();

            // overloading - parameter type is known during compile time
            Assert.AreEqual("zap object", baz.Zap("hello"));
            Assert.AreEqual("zap foo", baz.Zap(foo));


            // virtual call - single dispatch. Baz is used.
            Zapper zapper = baz;
            Assert.AreEqual("zap object", zapper.Zap("hello"));
            Assert.AreEqual("zap foo", zapper.Zap(foo));


            // C# has doesn't support multiple dispatch so it doesn't
            // know that oFoo is actually of type Foo.
            //
            // In languages with multiple dispatch, the type of oFoo will 
            // also be used in runtime so Baz.Zap(Foo) will be called
            // instead of Baz.Zap(object)
            object oFoo = foo;
            Assert.AreEqual("zap object", zapper.Zap(oFoo));
        }

        public class Zapper
        {
            public virtual string Zap(object o) { return "generic zapper" ; }
            public virtual string Zap(Foo f) { return "generic zapper"; }
        }

        public class Baz : Zapper
        {
            public override string Zap(object o) { return "zap object"; }
            public override string Zap(Foo f) { return "zap foo"; }
        }

        public class Foo { }
    }
}

这篇关于多态与多重分派有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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