为什么用更少/更大的参数重载函数?

发布于 2024-09-27 11:26:30 字数 154 浏览 11 评论 0原文

所以我是一名java程序员,我知道重载函数意味着什么。此外,我用不同类型的参数重载了一个函数,并且可以用更少和更大的参数重载。

我在一次采访中被问到这个问题。我真的不知道这是否有任何好处,也不知道面试官在这里得到了什么。它有什么性能优势吗?有什么想法吗?

谢谢。

So I am a java programmer and I know what overloading a function means. Moreover, I have overloaded a function with different type of arguments, and can overload with, fewer and greater arguments.

I was asked this on an interview. I really don't know if this has any benefits or what the interviewer was getting at here. Does it have any performance advantages? Any thoughts?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

庆幸我还是我 2024-10-04 11:26:30

它是通过允许使用默认值调用函数来提供灵活的接口。某些语言允许通过可选参数实现此目的,但您可以通过重载实现或多或少相同的效果。

It is about providing a flexible interface by allowing the functions to be called with default values. Some languages allow this via optional parameters, but you can achieve more or less the same with overloads.

泛泛之交 2024-10-04 11:26:30

我以前曾用它来提供向后兼容性。

I've used this to provide backward compatibilty before.

最偏执的依靠 2024-10-04 11:26:30

它提供灵活性
例如,

您有一个通常不需要更改的属性,但在特殊情况下,您可以简单地编写一个重载方法来接受特殊的情况,而不是要求它默认或要求每次在常规方法中设置它

假设

公共无效connectToDatabase(字符串connString)
{
//一些命令
}

公共无效connectToDatabase(字符串connString,字符串用户名,字符串密码)
{
//一些命令
}

its to provide flexibility
eg

say you have a proerty that usually doesnt need to be changed thexcept in special circumstances you could simply write an overloaded method to accept the special curcumstance instead of requiring it to be defaulted or requiring setting it everytime in the regular method

for example

public void connectToDatabase(string connString)
{
//some commands
}

public void connectToDatabase(string connString, string username, string password)
{
//some commands
}

灵芸 2024-10-04 11:26:30

面试官可能一直在暗示依赖注入,这可以通过方法或构造函数来完成。

The interviewer may have been hinting at dependency injection, which can be done through methods or constructors.

快乐很简单 2024-10-04 11:26:30

绝对是为了向后兼容。此外,如果很少有参数以非默认值传递(超过 2 个),那么创建重载方法很有用。

Definitely for backward compatibility. Also if few parameters are very rarely passed (more than 2) with non default values, then it is useful to create overloaded methods.

捂风挽笑 2024-10-04 11:26:30

如果一种语言不支持重载,那么执行速度会更快。因为程序将不必使用/跟踪/管理重载机制。

但如果语言支持重载,你应该使用它们!以下是性能方面的原因:

  1. 传递参数的成本很高。因为他们需要内存空间并对其进行访问。因此,如果您发送 2 个相同数据类型的参数,实际上会比传递 1 个参数慢。

  2. 对于不同的参数,你没有退路。

  3. 由于您至少必须为最长的参数列表创建函数,因此您必须检查大量 null/空值,并根据它们设置条件来实现针对更少或不同参数的算法。

  4. 有些参数实际上是其他参数的属性。您可以将它们以数组或数组引用的形式发送。就像您可能想要打印一些东西,并且您可能有一些常见的参数,例如颜色、边框等。您确实不需要为它们创建单个参数。

  5. 您还应该计算每个版本的使用次数。您可以合并频率较低的那些。不要碰最忙的!

    所以有很多实用的理论;)

If a language does not support overloading, then the execution is faster. Because the program will not have to use/track/manage the overloading mechanism.

But if the language support overloading, you should USE them! Here goes the reasons in sense of performance:

  1. Passing parameters are costly. Because they needs memory spaces and access to them. So if you send 2 parameters of the SAME data type, it will practically be slower than if you may pass 1 parameter.

  2. For different parameters, you have no way out.

  3. As you will at least have to make the function for the longest parameter list, you will have to check a lot of null/empty values and have conditions depending on them to implement the algorithm for fewer or different arguments.

  4. There are some arguments which are actually attributes of other arguments. You can send them in an array, or an array reference. Like you may want to print something and you may have some arguments like color, border, etc which are common to them. You really don't need to make single parameters for them.

  5. You should also count how many times each version is used. You can merge ones that may have lower frequencies. Don't touch the busiest ones!

    So a lot of practical theories ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文