Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 类只能是单个对象的模板,不能是集合的模板_Vb.net_Oop - Fatal编程技术网

Vb.net 类只能是单个对象的模板,不能是集合的模板

Vb.net 类只能是单个对象的模板,不能是集合的模板,vb.net,oop,Vb.net,Oop,我有一个简单的classList.vb,如下所示: Public Class List Public fList As List(Of Integer) Public Sub New() fList = New List(Of Integer) fList.Add(1) fList.Add(2) fList.Add(3) fList.Add(4) fList.Add(5) E

我有一个简单的class
List.vb
,如下所示:

Public Class List

    Public fList As List(Of Integer)
    Public Sub New()
        fList = New List(Of Integer)
        fList.Add(1)
        fList.Add(2)
        fList.Add(3)
        fList.Add(4)
        fList.Add(5)
    End Sub
End Class
Module Module1

    Sub Main()

        Dim fObject As List = New List
        Dim cnt As Integer = 0
        For Each x As Integer In fObject.fList
            Console.WriteLine("hello; {0}", fObject.fList.Item(cnt).ToString())
            cnt = cnt + 1
        Next

        Console.WriteLine("press [enter] to exit")
        Console.Read()

    End Sub

End Module
控制台
应用程序正在使用此类,如下所示:

Public Class List

    Public fList As List(Of Integer)
    Public Sub New()
        fList = New List(Of Integer)
        fList.Add(1)
        fList.Add(2)
        fList.Add(3)
        fList.Add(4)
        fList.Add(5)
    End Sub
End Class
Module Module1

    Sub Main()

        Dim fObject As List = New List
        Dim cnt As Integer = 0
        For Each x As Integer In fObject.fList
            Console.WriteLine("hello; {0}", fObject.fList.Item(cnt).ToString())
            cnt = cnt + 1
        Next

        Console.WriteLine("press [enter] to exit")
        Console.Read()

    End Sub

End Module
我是否可以更改类代码,使List.vb成为(整数)类型的列表?
这意味着在控制台代码中,我可以将fObject.fList中的
替换为fObject中的


还是我找错了树-类应该是单个对象,列表应该是类的集合?

是的,你可以这样做。为了使对象与每个
兼容,它必须具有
GetEnumerator
函数:

Public Function GetEnumerator() As IEnumerator _
  Implements IEnumerable.GetEnumerator
    Return New IntListEnum(fList)
End Function
IntListEnum
类必须依次实现
IEnumerator
,如下所示:

Public Class IntListEnum Implements IEnumerator

Private listInt As List(Of Integer)

Dim position As Integer = -1

Public Sub New(ByVal fList As List(Of Integer))
    listInt = fList
End Sub 

Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
    position = position + 1
    Return (position < listInt.Count)
End Function 

Public Sub Reset() Implements IEnumerator.Reset
    position = -1
End Sub 

Public ReadOnly Property Current() As Object Implements IEnumerator.Current
    Get 
        Try 
            Return listInt(position)
        Catch ex As IndexOutOfRangeException
            Throw New InvalidOperationException()
        End Try 
    End Get 
End Property 
Dim fObject As New List()
For Each x As Integer In fObject
    Console.WriteLine("hello; {0}", x)
Next

您可以看到一个完整的示例。

DasLinkenLight提供的答案非常好,但是如果您只需要一个预填充的整数列表,您可以从
列表(整数)
继承,然后让类在构造函数中填充自己:

Public Class List
    Inherits List(Of Integer)

    Public Sub New()
        Add(1)
        Add(2)
        Add(3)
        Add(4)
        Add(5)
    End Sub
End Class
当您从
List(Of Integer)
继承时,您的类将自动获得该类型实现的所有功能,因此您的类也将成为一个以相同方式工作的List类。然后,您可以这样使用它:

Public Class IntListEnum Implements IEnumerator

Private listInt As List(Of Integer)

Dim position As Integer = -1

Public Sub New(ByVal fList As List(Of Integer))
    listInt = fList
End Sub 

Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
    position = position + 1
    Return (position < listInt.Count)
End Function 

Public Sub Reset() Implements IEnumerator.Reset
    position = -1
End Sub 

Public ReadOnly Property Current() As Object Implements IEnumerator.Current
    Get 
        Try 
            Return listInt(position)
        Catch ex As IndexOutOfRangeException
            Throw New InvalidOperationException()
        End Try 
    End Get 
End Property 
Dim fObject As New List()
For Each x As Integer In fObject
    Console.WriteLine("hello; {0}", x)
Next

事实上,没有,说得对。我把它从我的例子中去掉了。由于
List(T)
类有一个接受零参数的构造函数,那么在您自己的
Sub-New
中的任何代码之前,将自动调用该基本构造函数。唯一需要显式调用MyBase.New的时间是调用一个带参数的基构造函数。。。。要将函数
GetEnumerator
添加到我的类列表中,我假设类需要
实现IEnumerable
?@我的理解是,尽管实现
IEnumerable
非常常见,但从语言的角度来看,它是可选的:“for each”即使没有实现接口,循环也可以调用
GetEnumerator
。如果我没有为调用类添加
Implements IEnumerable
,那么函数
GetEnumerator
(它是该类的一部分)显示行
实现IEnumerable.GetEnumerator
上的一个问题,带有消息
接口'System.Collections.IEnumerable'未由此类实现。
p.s完成得很好,达到100k@那么我猜VB和C有什么区别呢?