VB.Net OOP问题 - 超类,MyBase.net和构造函数参数

时间:2009-03-13 15:11:58

标签: vb.net oop

我有一个基类,它的New构造函数中包含相当多的参数。我有7个继承Super基类的子类。我的问题是,所有子类对基类的New构造函数中的大多数参数使用相同的值,并且可以一个接一个地调用这些子类。我想填充超类的公共值,然后为每个子类使用它们,但我不能想出一个很好的方法来做到这一点。如果我能弄清楚如何做到这一点,我就不必在每个子类的MyBase.New调用中传递所有这些参数。我会尝试用一些代码来说明......

Public Class BaseRequest

    Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)

        'Private Level Variables Assigned Here'

    End Sub

End Class

Public Class SubClass1
    Inherits BaseRequest


    Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)

        MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)    
    End Sub

End Class

Public Class SubClass2
    Inherits BaseRequest

    Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)

        MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)    
    End Sub

End Class

Public Class SubClass3
    Inherits BaseRequest

    Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)

        MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)    
    End Sub

End Class

Private Sub CallClasses

    If CallClass1 Then
        Dim sClass1 As New SubClass1(Param1Value, Param2Value, Param3Value, Param4Value,  Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
    End If

    If CallClass2 Then
        Dim sClass2 As New SubClass2(Param1Value, Param2Value, Param3Value, Param4Value,  Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
    End If

    If CallClass3 Then
        Dim sClass3 As New SubClass3(Param1Value, Param2Value, Param3Value, Param4Value,  Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
    End If

End Sub

我想摆脱每个子类中对“MyBase.New”的冗余调用,并且必须填充参数,因为我新建了对SubClasses的调用。换句话说,做这样的事情......

Dim MySuperClass as New BaseRequest(Param1Value, Param2Value, Param3Value, Param4Value,  Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)

If CallClass1 Then
    Dim sClass1 As SubClass1

    sClass1 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If

If CallClass2 Then
    Dim sClass2 As SubClass2
    sClass2 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If

If CallClass3 Then
    Dim sClass3 As SubClass3
    sClass3 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If

我希望这是可以理解的。最好的方法是什么?

3 个答案:

答案 0 :(得分:3)

Harpo是正确的。

然而,这种情况听起来像是工厂设计模式的一个很好的候选者。 您可以尝试将对象创建封装到另一个[Factory]类中,该类具有返回类型BaseRequest。然后,此工厂方法可以确定要创建的对象类型,然后您可以检查返回对象的类型。 e.g。

Public Class BaseRequestFactory

    Public Function CreateRequest(ByVal arg1 As Object,,,) As BaseRequest
        If condition1 Then
            Return New SubClass1(arg1,,,)
        Else If condition2 Then
            Return New SubClass2(arg1,,,)
        Else 
            ''etc
        End If
    End Function

End Class

最后一件事;你可能已经意识到,拥有大量参数的方法并不是一个好习惯。看看你是否可以使用get / set属性,或者考虑将另一个类的实例作为参数传递给构造函数。

答案 1 :(得分:1)

您当前的方法是最好的方法。你应该接受并继续前进。对MyBase.New的调用不是“多余的” - 他们每个人都说不同的东西,如果没有他们,你就不会得到你想要的行为。如果你看一下框架本身的子类构造函数,你会发现它以同样的方式完成。

答案 2 :(得分:0)

如果使用继承,那么实际上没有其他方法可以传递参数。但是,您可能会发现使用合成更合适。尝试使基类具体化并将其作为参数传递给其他类。