'Visual Basic 2010 Redefinition - Invariance, Covariance en Contravariance Static Typing en Dynamic Binding Class Fruit Public Function MethodA() As String End Function End Class Class Apple : Inherits Fruit Public Function MethodB() As String End Function End Class Class GrannySmith : Inherits Apple Public Function MethodC() As String End Function End Class Class JonaGold : Inherits Apple Public Function MethodD() As String End Function End Class Class BaseClass Public Overridable Sub Method1(ByVal argument As Apple) ' (3) End Sub End Class Class DerivedClass : Inherits BaseClass ' invariance ( unchanged parametertype ) : possible Public Overrides Sub Method1(ByVal argument As Apple) ' (6) End Sub ' contravariant ( supertype ) parametertype : expected, but not possible Public Overrides Sub Method1(ByVal argument As Fruit) ' (1) Console.WriteLine(argument.MethodA()) ' (4) End Sub ' covariant ( subtype ) parametertype : as expected not possible 'Public Overrides Sub Method1(ByVal argument As GrannySmith) ' (7) 'End Sub End Class Class ExpectedContraVariance Public Shared Sub Main() Dim grannySmith1 As GrannySmith = New GrannySmith Dim jonaGold1 As JonaGold = New JonaGold ' Dim object1 As BaseClass = New DerivedClass ' (5) ' object1.Method1(grannySmith1) ' (2) 'object1.Method1(jonaGold1) ' (8) End Sub End Class Partial Class BaseClass Public Overridable Function Method2() As Apple Method2 = New Apple End Function End Class Partial Class DerivedClass : Inherits BaseClass ' invariance ( unchanged returntype ) : possible Public Overrides Function Method2() As Apple Method2 = New Apple End Function ' covariant ( subtype ) returntype : expected, but not possible Public Overrides Function Method2() As GrannySmith Method2 = New GrannySmith End Function ' contravariant ( supertype ) returntype : as expected not possible 'Public Overrides Function Method2() As Fruit ' Method2 = New Fruit ' (1) 'End Function End Class Class ExpectedCoVariance Public Shared Sub Main() Dim grannySmith1 As GrannySmith = New GrannySmith Dim jonaGold1 As JonaGold = New JonaGold ' Dim object1 As BaseClass = New DerivedClass ' (5) ' Console.WriteLine(object1.Method2().MethodB()) End Sub End Class 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.