方法重载指南
当类包含两个名称相同但签名不同的方法时发生方法重载。本节提供一些使用重载方法的指南。
用方法重载来提供在语义上完成相同功能的不同方法。
使用方法重载而不是允许默认参数。默认参数的版本控制性能不好,因此公共语言规范 (CLS) 中不允许使用默认参数。下面的代码示例阐释重载的 String.IndexOf 方法。
[Visual Basic]
Function String.IndexOf(name As String) As Integer
Function String.IndexOf(name As String, startIndex As Integer) As Integer
[C#]
int String.IndexOf (String name);
int String.IndexOf (String name, int startIndex);
正确使用默认值。在一个重载方法系列中,复杂方法应当使用参数名来指示从简单方法中假定的默认状态发生的更改。例如,在下面的代码中,第一个方法假定搜索将不区分大小写。第二个方法用名称 ignoreCase 而不是 caseSensitive 来指示正在如何更改默认行为。
[Visual Basic]
' Method #1: ignoreCase = false.
Function Type.GetMethod(name As String) As MethodInfo
' Method #2: Indicates how the default behavior of method #1
' is being changed.
Function Type.GetMethod(name As String, ignoreCase As Boolean) As MethodInfo
[C#]
// Method #1: ignoreCase = false.
MethodInfo Type.GetMethod(String name);
// Method #2: Indicates how the default behavior of method #1 is being // changed.
MethodInfo Type.GetMethod (String name, Boolean ignoreCase);
对方法参数使用一致的排序和命名模式。提供一组重载方法,这组重载方法带有递增数目的参数,以使开发人员可以指定想要的级别的信息,这种情况很常见。您指定的参数越多,开发人员就可指定得越详细。在下面的代码示例中,重载的 Execute 方法具有一致的参数顺序和命名模式变体。每个 Execute 方法变体都对共享的参数集使用相同的语义。
[Visual Basic]
Public Class SampleClass
Private defaultForA As String = "default value for a "
Private defaultForB As Integer = "42 "
Private defaultForC As Double = "68.90 "
Overloads Public Sub Execute()
Execute(defaultForA, defaultForB, defaultForC)
End Sub
Overloads Public Sub Execute(a As String)
Execute(a, defaultForB, defaultForC)
End Sub
Overloads Public Sub Execute(a As String, b As Integer)
Execute(a, b, defaultForC)
End Sub
Overloads Public Sub Execute(a As String, b As Integer, c As Double)
Console.WriteLine(a)
Console.WriteLine(b)
Console.WriteLine(c)
Console.WriteLine()
End Sub
End Class
[C#]
public class SampleClass
{
readonly string defaultForA = "default value for a ";
readonly int defaultForB = "42 ";
readonly double defaultForC = "68.90 ";
public void Execute()
{
Execute(defaultForA, defaultForB, defaultForC);
}
public void Execute (string a)
{
Execute(a, defaultForB, defaultForC);
}
public void Execute (string a, int b)
{
Execute (a, b, defaultForC);
}
public void Execute (string a, int b, double c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine();
}
}
请注意,组中唯一的虚拟方法应该是具有大多数参数的方法,并且只应在需要可扩展性时才应有虚拟方法。
如果必须提供重写方法的能力,请仅使最完整的重载是虚拟的并根据它来定义其他操作。下面的示例阐释了这种模式。
[Visual Basic]
Public Class SampleClass
Private myString As String
Public Sub New(str As String)
Me.myString = str
End Sub
Overloads Public Function IndexOf(s As String) As Integer
Return IndexOf(s, 0)
End Function
Overloads Public Function IndexOf(s As String, startIndex As
Integer) As Integer
Return IndexOf(s, startIndex, myString.Length - startIndex)
End Function
Overloads Public Overridable Function IndexOf(s As String,
startIndex As Integer, count As Integer) As Integer
Return myString.IndexOf(s, startIndex, count)
End Function
End Class
[C#]
public class SampleClass
{
private string myString;
public MyClass(string str)
{
this.myString = str;
}
public int IndexOf(string s)
{
return IndexOf (s, 0);
}
public int IndexOf(string s, int startIndex)
{
return IndexOf(s, startIndex, myString.Length - startIndex );
}
public virtual int IndexOf(string s, int startIndex, int count)
{
return myString.IndexOf(s, startIndex, count);
}
}
带有可变数目的参数的方法
您可能希望公开带可变数目的参数的方法。C 编程语言中的 printf 方法是一个经典的示例。对于托管类库,请对此构造使用 params(Visual Basic 中为 ParamArray)关键字。例如,使用以下代码代替几个重载的方法。
[Visual Basic]
Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, params object [] args)
不应以独占方式使用 VarArgs 或省略号 (...) 调用约定,公共语言规范不支持它。
对于极度性能敏感的代码,您可能希望为少量元素提供特殊代码路径。只有在打算将整个代码路径作为特殊情况处理(而不只是创建数组和调用更为通用的方法)时才可以这么做。在这种情况下,建议使用下面的模式来在性能和特殊处理的代码的成本之间进行协调。
[Visual Basic]
Sub Format(formatString As String, arg1 As Object)
Sub Format(formatString As String, arg1 As Object, arg2 As Object)
Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, object arg1)
void Format(string formatString, object arg1, object arg2)
void Format(string formatString, params object [] args)
引用CSDN.... |