OOPS概念中方法重载的优点是什么 [英] What is the advantage of Method Overloading in OOPS concept

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

问题描述

任何人都可以告诉我方法重载的优点.我知道方法重载意味着方法具有相同的名称但参数列表不同.同样的事情也可以使用不同的函数名来完成,这两种方式都需要相同的编译时间和其他资源.
因此,请从编程的角度告诉我方法重载的主要优点.谢谢

Can anyone please tell me the advantage of method overloading. I know that method overloading means methods having same name but different argument lists. This same thing can be done using different function names also.And both these ways require same compilation time and other resources also.
Hence please tell me the major advantage of method overloading in programming point of view.. Thank You

推荐答案

您更希望记住:一个方法名,具有6个重载,或6个不同的方法名称:
Which would you rather remember: one method name, with 6 overloads, or 6 different method names:
ComputeFromDataTableLengthWidthAndHeight
ComputeFromDictionaryLengthWidthAndHeight
ComputeFromDataTableWidthAndHeight
ComputeFromDictionaryWidthAndHeight
ComputeFromDataTableAndHeight
ComputeFromDictionaryAndHeight

您想输入哪一个?还是阅读代码?

在过去,我们限制了所有内容,因此您会获得一系列名称相似的函数:

And which would you rather type? Or read in the code?

In the old days, we had limited everything, so you got a sequence of functions with similar names:

ComputeDTLWH
ComputeDLWH
ComputeDTWH
ComputeDWH
ComputeDTH
ComputeDH

犯错误太容易了!


好吧,我能想到的一个明显的优点是,您不必为它的名字思考每次过载...
例如,.NET中的MessageBox.Show有21(!)重载.想象一下有21种方法都可以做相同的事情.
MessageBox.ShowWithTextOnly
MessageBox.ShowWithTextAndCaption
MessageBox.ShowWithTextAndCaptionsAndIcon
MessageBox.ShowWithTextAndCaptionsAndIconAndButtons
...
我想你明白了:)
基本上,这里要做的是提高一致性和可读性.
此外,如果不支持重载,您将如何使用不同的参数调用构造函数?
Well, one obvious advantage I can think of is that you don''t have to think for a name for every overload...
For example, MessageBox.Show in .NET has 21(!) overloads. Imagine having 21 methods that all do the same thing.
MessageBox.ShowWithTextOnly
MessageBox.ShowWithTextAndCaption
MessageBox.ShowWithTextAndCaptionsAndIcon
MessageBox.ShowWithTextAndCaptionsAndIconAndButtons
...
I think you get the point :)
Basically, what it does here is improve consistency and readability.
Besides, how are you going to call a constructor with different parameters if overloading is not supported?


感谢您的解决方案...
Thank you for your solution...


这篇关于OOPS概念中方法重载的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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