是编译时如何超载和压倒一切的运行? [英] How Overloading is Compile Time and Overriding is Runtime?

查看:146
本文介绍了是编译时如何超载和压倒一切的运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乡亲

我碰到很多线程理解多态性(编译时和运行时)。我很惊讶地看到一些链接,其中的程序员都声称是超载运行和压倒一切的编译时间。

I came across many threads for understanding polymorphism (Both compile time and run time). I was surprised to see some links where the programmers are claiming Overloading is Runtime and Overriding is compile time.

我想知道,从这里是:

  1. 在运行时多态性与实时的例子和小code和什么情况下我们应该使用。
  2. 在编译时多态性与实时的例子和小code和时使用。

由于我读了许多理论上的定义,但我不满意理解这一点。

Because I read many theoretical definitions, but I am not satisfied in understanding that.

另外,我给了一个念头,那在这里我也感觉到了,超载应该是运行时的,因为,说我有一个计算区域,在运行时的方法只有它决定哪些重载方法调用基于我传递参数(说,如果我传递只有一个参数,它应该火广场,如果参数为2,它应该火矩形)......所以是不是我可以声称它的运行?如何将其complie时间? (最有发言权理论上,重载是编译的时候,但他们甚至不给出一个正确的实时例子......极少数声称其运行时)......

Also, I gave a thought, that where I also felt, overloading should be runtime as because, say I have a method that calculates Area, at runtime only it decides which overloaded method to call based on parameters I pass (Say if I pass only one parameter, it should fire Square, and if parameters are 2, it should fire Rectangle)....So isn't it I can claim its runtime ? How its complie time ? (Most say theoretically, overloading is compile time but they dont even give a correct REAL time example...very few claim its runtime)....

另外,我觉得首要的是编译时间,因为,当你写code和complie,可以确保你使用虚拟的关键字,并覆盖该方法在派生类中,否则会给你编译时错误。所以我觉得它的编译时间,在那里我在一个线程中看到了同样的方式.....但多数线程声称其运行时:D

Also, I feel overriding is compile time because, while you write code and complie, you ensure you used virtual keyword and also overriding that method in derived class which otherwise would give you compile time error. So I feel its compile time, the same way where I saw in a thread.....But most threads claims its runtime :D

我很困惑:(这个问题是附加到我的问题1和2。请帮助一个实时的例子..因为我已经知道的理论定义的....:(

I am confused :( This question is additional to my question 1 and 2. Please help with a real time example.. as I am already aware of theoretical definitions .... :(

感谢你......

推荐答案

在超载的情况下,您使用的是静态的(编译时)多态性因为编译器知道究竟要调用哪个方法。例如:

In the case of Overloading, you are using static (compile-time) polymorphism because the compiler is aware of exactly which method you are calling. For example:

public static class test
{
    static void Main(string[] args)
    {
        Foo();
        Foo("test");
    }

    public static void Foo()
    {
        Console.WriteLine("No message supplied");
    }

    public static void Foo(string message)
    {
        Console.WriteLine(message);
    }
}

在这种情况下,编译器知道到底是哪个美孚()方法,我们要求的基础上,参数的数量/类型。

In this case, the compiler knows exactly which Foo() method we are calling, based on the number/type of parameters.

重写是动态(运行时)多态性的一例)。这是由于这样的事实,编译器不一定知道什么类型的对象是在编译时被传递。假设你有以下的类库:

Overriding is an example of dynamic (runtime) polymorphism). This is due to the fact that the compiler doesn't necessarily know what type of object is being passed in at compile-time. Suppose you have the following classes in a library:

public static class MessagePrinter
{
    public static void PrintMessage(IMessage message)
    {
        Console.WriteLine(message.GetMessage());
    }
}

public interface IMessage
{
    public string GetMessage();
}

public class XMLMessage : IMessage
{
    public string GetMessage()
    {
        return "This is an XML Message";
    }
}

public class SOAPMessage : IMessage
{
    public string GetMessage()
    {
        return "This is a SOAP Message";
    }
}

在编译的时候,你不知道该函数的调用者传递一个XMLMessage之外,SOAPMessage,或可能是另一种类型的即时聊天的其他地方定义。当PrintMessage()函数调用,它决定在运行时使用的GetMessage函数()的版本,基于即时聊天的传入的类型。

At compile time, you don't know if the caller of that function is passing in an XMLMessage, a SOAPMessage, or possibly another type of IMessage defined elsewhere. When the PrintMessage() function is call, it determines which version of GetMessage() to use at runtime, based on the type of IMessage that is passed in.

这篇关于是编译时如何超载和压倒一切的运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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