Nothing 2005-8-23 10:18
8月25日,VB讨论如何用类编程
8月25日,VB讨论如何用类编程
创建自己的类
如果是一位有经验的编程者,经过多年的编程已经积累了一个有用的函数库。对象并不会代替函数- 实用程序的函数仍然需要编写和使用- 但对象对于组织过程和数据提供了一个方便和逻辑的方法。
特别是,借以创建对象的类能将数据和过程组织成一个整体。以下的主题解释了类如何增强代码的活力,如何导致新的编程方法的产生。
类:将用户定义类型和过程组织在一起 类是具有态的用户定义类型。封装将代码和数据组织在同一个模块中,使得对象能保护和验证其中数据的。
定制窗体类 多年前已创建过类- 当设计窗体时就创建了类。这对窗体设计都是有意义的成果。
类模块的循序渐进 简要介绍类模块,包括类的创建、从类创建的对象和对象的存活期规则。
调试类模块 描述类模块错误捕获选项中的断点,加上用 ALT+F8 键和 ALT+F5 键进行单步跟踪或运行先前的错误。
Visual Basic 窗体的存活期 窗体及其包含控件的存活期,与其它对象的存活期所遵循的规则有轻微的不同。
类模块与标准模块的比较 类模块和标准模块有显著的差别。理解这些差别有助于为对象编写更好的代码。
Nothing 2005-8-25 01:20
请大家准备好VB6.0,到时我将以一个学生管理系统进行讨论。
Nothing 2005-8-25 20:04
CStudent类模块
Dim iID As Long
Dim iName As String
Dim iSex As enSex
Dim iAge As Integer
Dim iAddress As String
Public Property Get ID() As Long
ID = iID
End Property
Public Property Let ID(ByVal vNewValue As Long)
iID = vNewValue
End Property
Public Property Get Name() As String
Name = iName
End Property
Public Property Let Name(ByVal vNewValue As String)
iName = vNewValue
End Property
Public Property Get Sex() As enSex
Sex = iSex
End Property
Public Property Let Sex(ByVal vNewValue As enSex)
iSex = vNewValue
End Property
Public Property Get Address() As String
Address = iAddress
End Property
Public Property Let Address(ByVal vNewValue As String)
iAddress = vNewValue
End Property
Public Property Get Age() As Integer
Age = iAge
End Property
Public Property Let Age(ByVal vNewValue As Integer)
iAge = vNewValue
End Property
Nothing 2005-8-25 20:38
CStudents类模块
Option Explicit
Private mcolStudents As New Collection
Dim intNum As Integer
Dim intPosition As Long
Dim iStudent As CStudent
Public Function Add(ByVal Name As String) As CStudent
Dim Student As New CStudent
With Student
'为学生产生一个唯一的 ID。
intNum = intNum + 1
.ID = intNum
.Name = Name
'将 CStudent 对象引用添加到集合中,
'用 ID 属性作为键。
mcolStudents.Add Student, "K" & .ID
End With
'为新的 CStudent 返回一个引用。
Set Add = Student
Set iStudent = Student
End Function
Public Function Count() As Long
Count = mcolStudents.Count
End Function
'删除其中的一个学生记录
Public Sub Delete(ByVal Index As Long)
mcolStudents.Remove "K" & Index
End Sub
'CStudents 集合类的方法。
Public Function Item(ByVal Index As Variant) As CStudent
'On Error Resume Next
Set Item = mcolStudents.Item("K" & Index)
End Function
'清除所有学生记录
Public Sub Clear()
Set mcolStudents = Nothing
Set mcolStudents = New Collection
intNum = 0
End Sub
Public Property Get Position() As Long
Position = intPosition
End Property
Public Property Let Position(ByVal vNewValue As Long)
If vNewValue < 1 Or vNewValue > mcolStudents.Count Then
Exit Property
End If
intPosition = vNewValue
Set iStudent = mcolStudents.Item("K" & intPosition)
End Property
Public Sub Move(Index As Long)
If Index < 1 Or Index > mcolStudents.Count Then
Exit Sub
End If
Set iStudent = mcolStudents.Item("K" & Index)
intPosition = Index
End Sub
Public Property Get SelectStudent() As CStudent
Set SelectStudent = iStudent
End Property
Nothing 2005-8-25 21:03
请大家到这里下载完整的代码
http://www.lihuasoft.net/source/show.php?id=3886