什么是方法重载? [英] What is method overloading?

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

问题描述

我发现有资源说方法重载是一种语言根据上下文使用相同方法但结果不同的能力. 不知何故,当我阅读其他定义时,我觉得那不是全部定义.方法重载还有更多吗?

I've found resources that say method overloading is the ability for a language to use the same method with a different outcome, depending on context. Somehow, when I read other definitions, I fell like that's not the whole definition. Is there more to method overloading?

推荐答案

那只是描述它的一种非常通用的方式.方法重载允许您使用单个方法名,但是重载"(提供多个版本)取决于上下文"(通常是传入的参数的类型或数量).由于每种方法都是分开的,因此它们可能导致不同的结果".

That's just a very general way of describing it. Method overloading allows you to use a single method name, but "overload it" (provide more than one version) depending on "context" (which is typically the type or number of arguments passed in). Since each method is separate, they can cause a "different outcome".

例如,使用C#,您可以编写:

For example, using C#, you can write:

void Foo()  // Version with no arguments
{
}

void Foo(int arg) // Version with a single int
{
}

void Foo(string arg1, double arg2) // Version with string and double parameters
{
}

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

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