VB6.0中类聚集关系的实现_VB.net_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

VB6.0中类聚集关系的实现

作者:黑客防线网安VB教程基地 来源:黑客防线网安VB教程基地 浏览次数:0

本篇关键词:实现关系聚集Children
黑客防线网安网讯:----类之间最主要的关系有两种,它们是聚集(Aggregation)和继承(Generelization)。聚集表示类之间的关系是整体与部分的关系,例如一个家庭有一个父亲、一个母亲和若干个孩子。类之间的聚...
----类之间最主要的关系有两种它们是聚集(Aggregation)和继承(Generelization)聚集表示类之间的关系是整体与部分的关系例如一个家庭有一个父亲、一个母亲和若干个孩子类之间的聚集关系又称包含关系,一个类由若干个其他类组合而成,当该类的实例被创建后,组成它的各类的实例将自动被创建。
  
  ----下图描述了Family类与组成它的各类之间的关系。Family类与Father类、Mother类的关系是一对一的关系,而Family类与Child类的关系是一对多的关系。为了简化类之间的关系,我们增加了一个Children类,Children类是Child类的集合,因此Family类与Children类直接关联,形成一对一的关系。
  
  Family------------>Father
  |
  -------->Mother
  |
  -------->Childred------>child
  
  ----VB6.0对类聚集关系的实现提供了较好的支持。在下面的程序中,我们仅给出了与Falimy类、Children类、Child类的具体实现有关的代码,以此为例说明类聚集关系的实现方法。
  ----程序中定义了三个类模块:Falimy类模块,Children类模块,Child类模块。在Falimy类模块中,利用属性过程,Mother类、Father类、Children类被定义为Family类的只读属性。下面是Family类模块中声明部分的代码。
  
  OptionExplicit
  PrivatemFatherAsNewFather
  PrivatemMotherAsNewMother
  PrivatemChildrenAsNewChildren
  PublicPropertyGetFather()AsFather
  SetFather=mMother
  EndProperty
  PublicPropertyGetMother()AsMother
  SetMother()=mMother
  EndProperty
  PublicPropertyGetChildren()AsChildren
  SetChildren=mChildren
  EndProperty
  
  ----下面是Children类模块的代码,首先在类模块的说明部分创建了集合类Collection的实例mcolChildren。在定义公共方法Add时,通过引用mcolChildren的Add方法将新的Child对象添加到集合中,Children类被定义成Child类的集合。通过直接引用mcolChildren的属性Count定义了Children类的公共属性Count,通过直接引用mcolChildren的方法Item定义了Children类的公共方法Item。
  ----我们还可以根据需要实现其他的属性和方法。通过创建Children类,与Children类有关的所有代码都封装起来,使得Children类可以进一步重用,从而较好地体现了面向对象程序设计的原则。
  
  OptionExplicit
  PrivatemcolChildrenAsNewCollection
  PublicPropertyGetCount()AsLong
  Count=mcolChildren.Count
  EndProperty
  PublicFunctionAdd(ByValNameAsString,
  ByValBirthDayAsDate,ByValSexAsBoolean)AsChild
  DimempNewAsNewChild
  StaticintNumAsInteger
  WithempNew
  intNum=intNum 1
  .Name=Name
  .BirthDay=BirthDay
  .Sex=Sex
  mcolChildren.AddempNew
  EndWith
  SetAdd=empNew
  EndFunction
  PublicFunctionItem(ByValIndexAsVariant)AsChild
  SetItem=mcolChildren.Item(Index)
  EndFunction
  
  ----下面是Child类声明部分的代码。
  OptionExplicit
  PublicNameAsString
  PublicBirthDayAsDate
  PublicSexAsBoolean
  
  ----程序中还定义了一个窗体模块。在窗体上布置有三个文本框txtName、txtBirthDay、txtSex,一个列表框lstChildren,两个命令按纽cmdAddChild、cmdListChild。
  ----在窗体模块中首先创建了一个Family类的实例sbMain,Children类和Child类的实例也随之被创建。在事件过程中,仅通过引用sbMain的属性Children,我们就可以实现对Children类的各种操作。
  
  OptionExplicit
  PublicsbMainAsNewFamily
  PrivateSubcmdAddChild_Click()
  sbMain.Children.AddtxtName.Text,
  txtSalary.Text,txtSex.Text
  txtBirthDay.Text=""
  txtName.Text=""
  txtSex.Text=""
  EndSub
  PrivateSubcmdListChild_Click()
  DimempAsNewChild
  DimiAsLong
  lstChild.Clear
  Fori=1TosbMain.Children.Count
  Setemp=sbMain.Children.Item(i)
  lstChild.AddItememp.Name&","
  &emp.BirthDay&","emp.Sex
  Next
  EndSub->

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14543-1.html
网站维护教程更新时间:2012-04-03 01:08:49  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer