什么是功能重载的使用/优点? [英] What is the use/advantage of function overloading?

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

问题描述

功能重载的使用/优点是什么?

What is the use/advantage of function overloading?

推荐答案

IMO的主要优点是方法命名的一致性/在逻辑上执行非常相似的任务的功能,并通过接受不同的参数略有不同。这允许在多个实现中重复使用相同的方法名称。

IMO, the primary benefit is consistency in the naming of methods / functions which logically perform very similar tasks, and differ slightly in by accepting different parameters. This allows the same method name to be reused across multiple implementations.

重载:(好)

function Person[] FindPersons(string nameOfPerson) { ... }
function Person[] FindPersons(date dateOfBirth) { ... }
function Person[] FindPersons(int age, string dogsName) { ... }

优于唯一命名功能:(更差)

Are preferable to 'uniquely named' functions: (Worse)

function Person[] FindPersonsByName(string nameOfPerson) { ... }
function Person[] FindPersonsByDOB(date dateOfBirth) { ... }
function Person[] FindPersonsByAgeAndDogsName(int age, string dogsName) { ... }

这样编写一个调用/消耗这些功能的客户端的编码器可以在更高级别的概念思维(我需要找到一个人),并且不需要记住/找到一个有创意的函数名称。

This way the coder writing a client which calls / consumes these functions can operate at a higher level of conceptual thinking ("I need to find a person") and doesn't need to remember / locate a contrived function name.

使用静态类型,编译器将被保留根据使用参数匹配适用的过载。对于动态打字,在运行时将会发生相同的匹配,如果没有找到适当的匹配,可能会导致失败。

With static typing, the compiler will be left to match the applicable overload based on the usage parameters. For dynamic typing, this same match up will happen at run time, possibly resulting in failure if no appropriate match is found.

这篇关于什么是功能重载的使用/优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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