请教这样的类该如何定义?

billow_chentao 2010-03-08 04:24:18
我想定义一个“地区”类型,它就如TreeView控件的节点对象一样,有属性:代码、名称、上级地区(隶属于地区)、下级地区列表。
(上级地区就是一个“地区”类型,下级地区列表是“地区”类型集合)
我是按以下定义的:

Public Class 地区集合
Inherits System.Collections.ObjectModel.KeyedCollection(Of String, 地区)
Protected Overrides Function GetKeyForItem(ByVal item As 地区) As String
Return item.代码
End Function
End Class
Public Class 地区
Private _代码 As String
Private _名称 As String
Private _上级 As 地区
Private _下级 As 地区集合
Public Sub New()
Me._代码 = ""
Me._名称 = ""
Me._上级 = Nothing
Me._下级 = Nothing
End Sub
Public Sub Dispose()
Me._代码 = Nothing
Me._名称 = Nothing
Me._上级 = Nothing
Me._下级 = Nothing
End Sub
Public Property 代码() As String
Get
Return Me._代码
End Get
Set(ByVal value As String)
Me._代码 = value
End Set
End Property
Public Property 名称() As String
......
End Property
Public Property 上级() As 地区
......
End Property
Public Property 下级() As 地区集合
......
End Property
End Class


当我按以上代码定义后使用发现,我这样做出来同TreeView控件的节点对象并不一样。
dim d1 as new 地区
dim d2 as new 地区
d1.代码=“123”
d1.名称=“abc”
d2.代码=“123123”
d2.名称=“abcabc”
d1.下级.add(d2) '<---这里我想把d2放到d1的下级列表中去

这样后d1的属性“下级”这个集合里有了一个d2对象,但d2对象的属性“上级”并不是d1,而是Nothing
说明还需要我专为d2的“上级”属性,传一个d1进去

但我们在使用TreeView控件的节点对象时,并没有为每个子节点赋值父级节点呀!!当在节点上添加一个下级节点后,这个下级节点的上级节点就自动是当前节点了,她是如何做的呢??

这样的对象还不只"TreeView控件的节点对象"如DataSet对象等很多对象她们是如何实现的??
...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jetwangstar112 2010-03-10
  • 打赏
  • 举报
回复
这个树形结构需要用递归吗?
billow_chentao 2010-03-10
  • 打赏
  • 举报
回复
wuyazhe:
有什么比较好的方法呢?讨论一下。
兔子-顾问 2010-03-10
  • 打赏
  • 举报
回复
绝不觉得问题复杂化了?不需要用树,对么?链表?也用不着的。
billow_chentao 2010-03-10
  • 打赏
  • 举报
回复
递归是个什么意思呢?
billow_chentao 2010-03-09
  • 打赏
  • 举报
回复
我现在改成这样了,大家讨论一下
Public Class Area
Private _Code As String
Private _Name As String
Private _Parent As Area
Private _Children As AreaCollection
Public Sub New()
Me._Code = ""
Me._Name = ""
Me._Parent = Nothing
Me._Children = New AreaCollection(Me)
End Sub
Public Sub New(ByVal code As String, ByVal name As String)
Me._Code = code
Me._Name = name
Me._Parent = Nothing
Me._Children = New AreaCollection(Me)
End Sub
Public Property Code() As String
Get
Return Me._Code
End Get
Set(ByVal value As String)
Me._Code = value
End Set
End Property
Public Property Name() As String
Get
Return Me._Name
End Get
Set(ByVal value As String)
Me._Name = value
End Set
End Property
Public ReadOnly Property FullCode() As String
Get
If Me._Parent Is Nothing Then
Return Me._Code
Else
Return Me._Parent.FullCode & "." & Me._Code
End If
End Get
End Property
Public ReadOnly Property FullName() As String
Get
If Me._Parent Is Nothing Then
Return Me._Name
Else
Return Me._Parent.FullName & "." & Me._Name
End If
End Get
End Property
Public Property Parent() As Area
Get
Return Me._Parent
End Get
Set(ByVal value As Area)
Me._Parent = value
End Set
End Property
Public Property Children() As AreaCollection
Get
Return Me._Children
End Get
Set(ByVal value As AreaCollection)
Me._Children = value
End Set
End Property
End Class
Public Class AreaCollection
Public Enum FindProperty
Code = 1
Name = 2
CodeAndName = 3
End Enum
Private _Parent As Area = Nothing
Private _Children As List(Of Area)
Public Sub New()
Me._Parent = Nothing
Me._Children = New List(Of Area)
End Sub
Public Sub New(ByVal parentArea As Area)
Me._Parent = parentArea
Me._Children = New List(Of Area)
End Sub
Public ReadOnly Property Item() As List(Of Area)
Get
Return Me._Children
End Get
End Property
Public Function Add(ByVal code As String, ByVal name As String) As Area
Try
If code = "" Then Throw New Exception("代码不能为空。")
If name = "" Then Throw New Exception("名称不能为空。")
For Each are As Area In Me._Children
If are.Code = code Then Throw New Exception("代码[" & code & "]重复。")
If are.Name = name Then Throw New Exception("名称[" & name & "]重复。")
Next
Dim newArea As Area = New Area(code, name)
newArea.Parent = Me._Parent
Me._Children.Add(newArea)
Return newArea
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Public Sub Add(ByRef newArea As Area)
Try
If newArea.Code = "" Then Throw New Exception("代码不能为空。")
If newArea.Name = "" Then Throw New Exception("名称不能为空。")
For Each are As Area In Me._Children
If are.Code = newArea.Code Then Throw New Exception("代码[" & newArea.Code & "]重复。")
If are.Name = newArea.Name Then Throw New Exception("名称[" & newArea.Name & "]重复。")
Next
newArea.Parent = Me._Parent
Me._Children.Add(newArea)
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Sub
Public Sub Clear()
Me._Children.Clear()
End Sub
Public Sub Remove(ByVal p As FindProperty, ByVal v As String)
Try
Dim i As Integer
i = Me.FindIndex(p, v)
Me._Children.RemoveAt(i)
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Sub
Public Sub Remove(ByVal item As Area)
Try
Me._Children.Remove(item)
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Sub
Public Sub RemoveAt(ByVal index As Integer)
Try
Me._Children.RemoveAt(index)
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Sub
Public Function Find(ByVal p As FindProperty, ByVal v As String) As Area
Try
Dim i As Integer
i = Me.FindIndex(p, v)
If i = -1 Then
Return Nothing
Else
Return Me._Children(i)
End If
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function

Public Function FindIndex(ByVal p As FindProperty, ByVal v As String) As Integer
Try
If p = 0 Or v = "" Then Throw New Exception("条件不能为空。")
Dim i As Integer = -1
For Each are As Area In Me._Children
i += 1
Select Case p
Case FindProperty.Code
If are.Code = v Then Return i
Case FindProperty.Name
If are.Name = v Then Return i
End Select
Next
Return -1
Catch ex As Exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
End Class
清晨曦月 元老 2010-03-08
  • 打赏
  • 举报
回复
这个是树形结构吧,可以用链表来实现。去网上查查,很多代码,基本的意思就是设置一个根,然后把下级列表的引用保存在根上,然后再下一级的列表也如此实现。ADD时就可以知道到底是添加到哪个节点上,把这个节点保存在你的子节点上,也就是说D1.XJ.ADD这个代码需要重写你用的基类的ADD的方法,在调用基类ADD方法前,即可D2.SJ=ME了。你的定义方法里面恐怕很难实现D2.SJ=ME,因为你的ME都是一个地区集合,而在地区那里有无法显式声明一个ME.SJ=NEW 地区,会导致递归然后就溢出了。。。

lwd203 2010-03-08
  • 打赏
  • 举报
回复
想知道的话可以查查它们的源代码,以及看一些相关的demo

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧