新聞中心
經(jīng)過長時(shí)間學(xué)習(xí)VB.NET IEnumerator接口,于是和大家分享一下,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。在面向?qū)ο蟮脑O(shè)計(jì)中,經(jīng)常會(huì)用到有類似父子關(guān)系的這個(gè)對象,比如在我現(xiàn)在的一個(gè)項(xiàng)目中,有訂單對象,在一個(gè)訂單下又包含多個(gè)產(chǎn)品,這時(shí)我就想用Iterator模式來封裝訂單下的產(chǎn)品,在dot Net中的IEnumerator接口就是用來實(shí)現(xiàn)迭代的,來支持dot Net中的for each的操作。

我們擁有十多年網(wǎng)頁設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信開發(fā)、微信平臺小程序開發(fā)、移動(dòng)網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。
要實(shí)現(xiàn)VB.NET IEnumerator接口,需在實(shí)現(xiàn)以下幾個(gè)函數(shù)來支持VB.NET IEnumerator接口的操作
- Overridable ReadOnly Property Current() As Object
- 'Current用于在迭代過程中得到當(dāng)前的對象
- Public Overridable Function MoveNext() As Boolean
MoveNext用于在迭代過程中將迭代指針指向下一個(gè)對象,初始是迭代指針指向集合的開始(在第一個(gè)節(jié)點(diǎn)之前的位置),一旦越過集合的結(jié)尾,在調(diào)用 Reset 之前,對 MoveNext 的后續(xù)調(diào)用返回 false。
- Overridable Sub Reset()
#t#將枚舉數(shù)設(shè)置為其初始位置,該位置位于集合中第一個(gè)元素之前。
只要集合保持不變,枚舉數(shù)就將保持有效。如果對集合進(jìn)行了更改(例如添加、修改或刪除元素),則該枚舉數(shù)將失效且不可恢復(fù),并且下一次對 MoveNext 或 Reset 的調(diào)用將引發(fā) InvalidOperationException。
下面是一個(gè)具體的實(shí)現(xiàn)VB.NET IEnumerator接口的對像
- Imports System.Collections
- '在此實(shí)際實(shí)現(xiàn)的是System.Collections.IEnumerable接口,
IteratorProduct 用此接口來向使用者提供對IEnumerator接口的操作。- Public Class IteratorProduct : Implements System.Collections.IEnumerable
- Private Products As Collection '用Collection在存訂單中的所有產(chǎn)品
- Private item As Integer = -1
- Public Sub New()
- Products = New Collection
- Products.Add("xh") '這只是為了測試方便,將加入產(chǎn)品的內(nèi)容直接寫在這了
- Products.Add("lj")
- Products.Add("qd")
- End Sub
- Overridable ReadOnly Property Current() As Object
- Get
- Return Products(item)
- End Get
- End Property
- Public Overridable Function MoveNext() As Boolean
- item += 1
- End Function
- Overridable Sub Reset()
- item = -1
- End Sub
- '返回迭代對像給使用者
- Overridable Function GetEnumerator()
As IEnumerator Implements IEnumerable.GetEnumerator- Return Me.Products.GetEnumerator
- End Function
- End Class
- Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load- Dim Products As IteratorProduct
- Products = New IteratorProduct
- Dim ProductName As String
- For Each ProductName In Products
- Response.Write(ProductName)
- Response.Write("
")- Next
- End Sub
新聞名稱:詳細(xì)講述VB.NET IEnumerator接口
本文網(wǎng)址:http://fisionsoft.com.cn/article/dpedpop.html


咨詢
建站咨詢
