简体   繁体   English

功能过载

[英]function overload

Can I have two same function name with same parameters but different meaning. 我可以使用相同参数但含义不同的两个相同的函数名称。

For example: 例如:

public void test(string name) 

public void test(string age) 

Thank you. 谢谢。

No, you can't. 不,你不能。 The signature is not different - it doesn't matter what the parameter names are. 签名没有区别 - 参数名称是什么并不重要。

Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. 通过指定访问级别(如public或private),可选修饰符(如abstract或sealed),返回值,方法名称和任何方法参数,在类或结构中声明方法。 These parts together are the signature of the method. 这些部分一起是该方法的标志。

http://msdn.microsoft.com/en-us/library/ms173114.aspx http://msdn.microsoft.com/en-us/library/ms173114.aspx

Like a few other answers have stated, consider the type of data you're taking in. Name is indeed a typical string, but does age have to be? 就像其他几个答案所说的那样,考虑一下你所采用的数据类型。名称确实是一个典型的字符串,但是年龄必须是吗? If you allow it to be a - for example - int then you can overload your method as you wish. 如果你允许它是 - 例如 - int那么你可以根据需要重载你的方法。

No, you cannot overload on a return type or a parameter name. 不,您不能在返回类型或参数名称上重载。 Unlike some other languages (most notably, Objective C 1 ) parameter name is not part of the signature of your function. 与其他一些语言(最值得注意的是,Objective C 1 )不同,参数名称不是函数签名的一部分。

The signature of a method consists of the name of the method and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. 方法的签名包括方法的名称以及每个形式参数的类型和种类(值,引用或输出),按从左到右的顺序考虑。 The signature of a method specifically does not include the return type, nor does it include the params modifier that may be specified for the right-most parameter. 方法的签名特别不包括返回类型,也不包括可以为最右边的参数指定的params修饰符。


1 even there it's not exactly the parameter name that becomes part of the selector. 1即使在那里它也不是参数名称成为选择器的一部分。

您可以使用具有相同名称的静态和非静态方法,但遵循与方法重载相同的规则的不同参数,它们不能具有完全相同的签名。

No. Signatures and Overloading 编号和重载

If you need a method with different meaning why won't you create a method with a different name? 如果您需要具有不同含义的方法,为什么不创建具有不同名称的方法? It would be confusing to use the same method name for different things on the same object. 在同一对象上对不同的东西使用相同的方法名称会令人困惑。

You could mix together these methods using optional parameters and default values: 您可以使用可选参数和默认值将这些方法混合在一起:

public void test(string name = null, string age = null)
{
  if (name != null) 
  {
     // Do something
  }
  else if (age != null)
  {
     // Do something else
  }
}

And you could call this method like that: 你可以像这样调用这个方法:

test(name: "John");
test(age: "30");

Not very clean, but still useable. 不是很干净,但仍然可用。

否 - 编译器抛出错误,因为编译器使用参数来确定要调用的方法,而不是返回类型。

NO. 没有。

An OVERLOADED FUNCTION must have different SIGNATURE . 载功能必须具有不同的SIGNATURE ie- arguments should be different, either in terms of number of arguments or order of different datatypes arguments. ie-参数应该是不同的,无论是参数的数量还是不同数据类型参数的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM