为什么提出了函数重载的概念? [英] why Function overloading concept has been made?

查看:69
本文介绍了为什么提出了函数重载的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我知道这是非常基本的&愚蠢的问题,但如果有人能使我明白这一点.就像重载所说,同一个名称具有不同的功能..我的问题是...
假设我们有两个重载函数

Add(int a,int b);
Add(float a,float b);

所以我想知道这里是.为什么要重载函数?
我可以给两个函数起一个不同的名称,例如

AddIntegers(int a,int b);
AddFloats(float a,float b);

我知道阅读此书后会想到的事情是:
我们可以为不同的功能命名不同的范围.
我们如何保持一致性.

但是我相信除此之外,应该没有任何理由引入重载概念.

Hi All,

I know this is very basic & silly question but if anyone can clear me on this. As overloading says, same name different functionality..My question is...
suppose we have two overloaded functions

Add( int a, int b);
Add( float a, float b);

so i would like to know here is. Why should i overload a function?
i can give a different name to both the functions like

AddIntegers( int a, int b);
AddFloats( float a, float b);

I know the things that will come in our mind after reading this are:
How far we can give different name to different functionality.
How do we maintain a uniformity.

But i believe other than this there should be any reason to introduce overloading concept.

推荐答案

这不是一个愚蠢的问题:这是一个基本问题. />
您描述的第二种方式正是在OOP之前我们曾经做过的事情.
每次需要一组稍微不同的参数时,都需要一个新的函数名称.

可以,当您只有"int"和"float"时,但是当您的重载变得更加复杂时,就会发生这种情况:
It''s not a silly question: it is a fundamental question.

The second way you describe is exactly how we used to do it, before OOP.
Every time you needed a slightly different set of parameters, you needed a new function name.

It''s ok, when you have just "int" and "float", but whet happens when your overloads get more complex:
Add(int x1, int y1, int x2, int y2)
Add(Point p1, Point p2)

现在您需要一些稍微复杂的名称.

关于:

Now you need some slightly complicated names.

What about:

public static void Email(string body)
public static void Email(string body,
                         params MailAttachment[] attachments)
public static void Email(string to, string body)
public static void Email(string to,
                         string body,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject)
public static void Email(string to,
                         string body,
                         string subject,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)

(我刚刚从电子邮件utils文件中删除了该文件).
您开始给每个人打电话什么?以及如何记住您需要哪一个呢?

基本上,它们使代码更具可读性,并使逻辑流程更简单.

(Which I just ripped out of my email utils file).
What do you start calling each one? And how do you remember which one you need?

Basically, they make the code more readable, and make the logical flow simpler.


重载允许您定义不同的方法签名,因此您可以将许多不同的参数或数据类型传递给方法,

Overloading allows you to define a different method signature, so you to pass many different parameters or data types to a method,

public void OverloadTest()
{
    OverloadTest(0, string.Empty);
}

public void OverloadTest(int a)
{
    OverloadTest(a, string.Empty);
}

public void OverloadTest(string b)
{
    OverloadTest(0, b);
}

public void OverloadTest(int a, string b)
{
   Dosomething(a, b);
}



请注意,在上文中,只有一个函数实际上会对参数(Dosomething)执行任何操作.所有其他方法仅调用最贪婪"方法签名,并为任何缺失"参数值传递默认值.

由于采用了这种语法,因此以下内容可以正常工作



Notice in the above, only one function will actually do anything with the parameters (Dosomething). All of the other methods just call the ''greediest'' method signature and pass default values for any ''missing'' parameter values.

Because of this syntax, the following works OK

int a = 0;
OverloadTest(a);

string b = "hello";
OverloadTest(b);

OverloadTest(a, b);



现实生活中的一个很好的例子是ToString方法,在这里我们可以将各种数据类型传递给该方法,并且可以正常工作



A good real life example is the ToString method, where we can pass all sorts of Data Types to the method and it will work OK

int x = 10;
Console.WriteLine (x);

string Message = "Hello";
Console.WriteLine (Message);


假设您有一个名为Student
的类
您希望能够创建Student的实例.
您可以像下面这样重载默认构造函数:
Suppose you have a class called Student

You want to be able to create an instance of Student.
You can over load the default constructor like the following:
public Student()
{
}

public Student(string name)
{
}

public Student(int age)
{
}

public Student(string name, int age)
{
}



上面的代码提供了灵活性,可以通过从构造函数传递它们(即如果您事先知道它们)来设置类中的属性,也可以稍后再设置它们.此处的灵活性在于,在创建Student类的实例



The above code gives the flexibility to set the properties in the class either by passing them from the constructor (that is if you know them before hand) or you can set them later on. The flexibility here is that you would not be required to fill the parameter when you are creating the instance of the Student class


这篇关于为什么提出了函数重载的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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