我想知道是否可以在VB.Net 4中擁有依賴類型,或者,如果可以根據基類的構造函數的參數構造繼承對象。例如,VB.NET中的依賴類型
Class BaseClass
Sub New(type as String)
If type = "One" then
Me = New Child1 'Assignment to Me is syntax error, but it explains the concept...
Else
Me = New OtherChild
End If
End Sub
End Class
Class Child1
Inherits BaseClass
...
Class OtherChild
Inherits BaseClass
..
..
Sub Main()
Dim c1 As New BaseClass("One")
Dim c2 As New BaseClass("Two")
OverloadedMethod(c1) 'Outputs One
OverloadedMethod(c2) 'Outputs Two
End Sub
Sub OverloadedMethod(C as Class1)
Console.Write("One")
End Sub
Sub OverloadedMethod(C as OtherClass)
Console.Write("Two")
End Sub
EDIT:約相關類型說明:
Dependent types是其基於一些參數構造類型(例如一個標量值)。這在一些(主要是功能性)編程語言(如Haskell)中是一個衆所周知的概念。 例如,在支持依賴類型的一個假想的命令式語言,可以寫成:
Matrix(3,10) A; //A is a 10x10x10 3D Matrix
Matrix(2,3) B; //B is a 3x3 2D Matrix
然後
A(0,0,0) = 10;
B(0,0) = -2;
B(1,1,0) = 5; // Type Error