简体   繁体   English

VB.NET中的从属类型

[英]Dependent types in VB.NET

I was wondering if it is possible to have dependent types in VB.Net 4, or alternatively, if it is possible to construct inherited objects based on the base class' constructor's parameter(s). 我想知道在VB.Net 4中是否可以具有依赖类型,或者是否可以基于基类的构造函数的参数构造继承的对象。 For example, 例如,

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 : Explanations about Dependent Types : 编辑 :有关从属类型的说明:

Dependent types are types which are constructed based on some parameters(eg an scalar value). 从属类型是基于某些参数(例如标量值)构造的类型。 That is a well-known concept in some (mainly functional) programming languages(eg Haskell). 在某些(主要是功能)编程语言(例如Haskell)中,这是一个众所周知的概念。 For example, in a hypothetical imperative language which supports dependent types, one can write: 例如,在一种支持依赖类型的假设命令式语言中,可以编写:

Matrix(3,10) A; //A is a 10x10x10 3D Matrix
Matrix(2,3)  B; //B is a 3x3 2D Matrix

And then 接着

A(0,0,0) = 10;
B(0,0) = -2;
B(1,1,0) = 5; // Type Error

EDIT : Considering your comments, I wasn't aware about dependent types. 编辑:考虑到您的评论,我不知道依赖类型。 It seems those are not yet implemented in any object oriented programming language. 那些语言似乎尚未以任何面向对象的编程语言实现。 There are research about combining the two of them, bu it seems we're not yet at the step of implementation AFAIK. 有关于将两者结合的研究 ,但似乎我们还没有实现AFAIK的步骤。

At the moment, a constructor cannot construct anything else than the type it is constructing (in your example BaseClass ). 目前, 构造函数除了构造的类型(在您的示例BaseClass )之外不能构造其他任何东西。

The closest you could get would be with an abstract factory . 最接近的可能是抽象工厂 That would look like this (I kept the string, and made the method static for simplification, but it is usually not part of the pattern, read about it for more details) : 看起来像这样(为了简化起见,我保留了字符串,并使该方法静态化,但是它通常不是模式的一部分,请阅读有关更多详细信息):

   Class ClassFactory
      Public Shared Function GenerateBaseClassObject(type As String) As BaseClass
         If type = "One" Then
            Return New Child1  
         Else
            Return New OtherChild
         End If
      End Function
   End Class

On the other part of your question, the overloading, that would just make things harder to define your methods outside of your object hierarchy. 在问题的另一部分,重载只会使在对象层次结构之外定义方法变得更加困难。 As you can see, the abstract factory return a BaseClass , and no one knows outside of the factory, and the constructed objects, what is their real type (if we don't consider reflection and casting, that would just be confusing at this point). 如您所见,抽象工厂返回一个BaseClass ,在工厂外部没有人知道,并且构造的对象的真实类型是什么(如果我们不考虑反射和强制转换,那么这会令人困惑)。 You should rework your object hierarchy to encapsulate the differences between your two kind of classes. 您应该重新设计对象层次结构,以封装两种类之间的差异。

A complete working refactoring of your code would look like this : 代码的完整工作重构如下所示:

MustInherit Class BaseClass
      Public MustOverride Sub Output()
   End Class

   Class Child1
      Inherits BaseClass

      Public Overrides Sub Output()
         Console.Write("One")
      End Sub
   End Class

   Class OtherChild
      Inherits BaseClass

      Public Overrides Sub Output()
         Console.Write("Two")
      End Sub
   End Class

   Class ClassFactory
      Public Shared Function GenerateBaseClassObject(type As String) As BaseClass
         If type = "One" Then
            Return New Child1
         Else
            Return New OtherChild
         End If
      End Function
   End Class

   Sub Main()
      Dim c1 As BaseClass = ClassFactory.GenerateBaseClassObject("One")
      Dim c2 As BaseClass = ClassFactory.GenerateBaseClassObject("Two")
      c1.Output()
      c2.Output()
      Console.ReadLine()
   End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM