为什么在面向对象编程中需要函数重载 [英] why function overloading needed in Object oriented programming

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

问题描述

大家好,



这是Sachin,一个oop的新手,问一个非常愚蠢的问题,但需要一个解释来避免大的混乱。



为什么面向对象编程需要函数重载?

推荐答案

不需要 - 不是一点都不函数重载只是为了方便,只是为了实际不同的方法允许相同的名称。编译器可以在可能的情况下通过查看调用的参数来告诉另一个方法。



正式地,我完全回答了这个问题。但是...



OOP上使用的是覆盖。这是OOP的中心点。如果不理解OOP的主要思想,就无法解释它。请参阅该主题的任何手册 - 这有点超出了CodeProject的格式。这只是一个学校考试或面试问题,专家通常不会回答这些问题。



-SA
It is not needed — not at all. Function overloading is only for convenience, just to allow same name for actually different methods. Compiler can tell one method from another just by looking at the parameters of the call, where possible.

Formally, I answered this question fully. However…

What is used on OOP is overriding. This is a central point of OOP. You cannot explain it without understanding the main idea of OOP. See any manual of the topic — this is a bit beyond the format of CodeProject. It this is just a school test or interview question, experts usually don't answer such Questions.

—SA


嘿,亲爱的先试试谷歌,然后问问题

Google [ ^ ]



但让我解释一些逻辑和一些有趣的概念,

函数重载意味着具有不同参数列表的相同函数,例如,

跟随

Hey dear first try google for this then asked question
Google[^]

but let me explain some logic and some interesting concept,
A Function overloading means same function with different parameter list like,
following
add(int k,int j)  //for adding two integer

add(float k,float j)//for adding two floats 





现在在java中举例,



Now in java take example,

int indexOf(String str) //take one parameter
int indexOf(int ch, int startIndex) //overloaded with two parameter



现在如果没有函数重载那么同样的任务有两个saparates函数。

就像那里有不同目的的增加所以

为减少相同任务的增加顺序,运算符重载引入,

与构造函数重载相同


now if there no function overloading then for same task there two saparates functions.
Like there goes increase for different purpose so
To reduce the increase order of function for same task a operator overloading introduced,
same with constructor overloading


让生活更轻松对于我们所有人。



如果我构建一个通用的电子邮件方法,我将构造它有一些重载:

To make life easier for us all.

If I construct a generic email method, I will construct it with a number of overloads:
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)

这些都将最终调用最后一个,这将完成实际工作。所有其他人只是用适当的默认值填空或准备合适的参数。



在过去,我将不得不构建九个不同的例程,所有这些都有不同的名称,如EmailToBodySubjectFromaddressWithAttachments,然后拼命记住哪些参数,或如何构建相关的默认值。这样,我必须记住一个名字:电子邮件,系统将帮助我填补空白。



对我来说更容易,还有更多可靠启动!

And these will all eventually call the final one, which will do the actual job. All the others just "fill in the blanks" with appropriate defaults, or prepare suitable parameters.

In the old days, I would have had to construct nine different routines, all with different names such as "EmailToBodySubjectFromaddressWithAttachments", and then try desperately to remember which had which parameters, or how to construct the relevant defaults. This way, I have to remember one name: "Email" and the system will help me fill in the blanks.

Much easier for me, and a whole lot more reliable to boot!


这篇关于为什么在面向对象编程中需要函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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