【发布时间】:2011-09-14 17:08:26
【问题描述】:
我想知道在 VB.Net 4 中是否可以有依赖类型,或者是否可以根据基类的构造函数参数构造继承的对象。例如,
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
编辑:关于依赖类型的解释:
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
【问题讨论】:
标签: vb.net inheritance overloading