0

これは、オブジェクト指向、特に .NET (またはその他のフレームワークや言語) での関数のオーバーロードに関する一般的な質問です。重複コードが多いアプリケーションを見ています。たとえば、次の関数を見てください。

Public Function Test(ByVal Test1 As String)

//code that is specifically relevant to Test1 variable

End Function

Public Function Test (ByVal Test1 As String, ByVal Test2 As String)
    //code that is specifically relevant to Test1 variable
    //code that is specifically relevant to Test2 variable
End Function

最良の方法は次のようにすることだと思います。これは事実ですか?私は常に、コードの重複は非常に悪い考えだと考えてきました。

4

2 に答える 2

3

それは良くありません:

Public Function Test(ByVal Test1 As String) 
    //code that is specifically relevant to Test1 variable
End Function 

Public Function Test (ByVal Test1 As String, ByVal Test2 As String) 
    Test(Test1)
    //code that is specifically relevant to Test2 variable 
End Function 

オーバーロードは、理想的には、元の関数に追加の機能を追加することですが、コード内の別の場所で使用する場合に備えて、元の動作を維持することです。

于 2012-05-18T20:57:38.103 に答える
1

通常、可能な場合は次のようにします。

Public Function Test(ByVal Test1 As String)
    Test(Test1, Nothing)
End Function

Public Function Test (ByVal Test1 As String, ByVal Test2 As String)
    ' code that is specifically relevant to Test1 variable
    If Test2 IsNot Nothing Then
        ' code that is specifically relevant to Test2 variable
    End If
End Function
于 2012-05-18T20:50:43.507 に答える