日期:2012-11-02  浏览次数:20457 次

1. 类别的「程序成员」(Procedure Member)

类别 (Class)之任务是把资料(Data)和程序(Procedure)组织并封装起来。类别告诉计算机﹕「其对象应含有那些资料、应含有那些程序裨处理外界传来之讯息」。类别须详细说明它的资料及程序﹐我们称此资料是类别之「资料成员」(Data Member) ﹔而称此程序是类别之「程序成员」(Procedure Member)。有关类别内容之叙述﹐就是所谓的类别定义(Class Definition)。类别定义之格式为──

类别之用途为﹕宣告对象。例如﹕

'ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'----------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
End Class
'-----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO:Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
.......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal
e As System.EventArgs)
Dim a As New Tree()
MsgBox("Object a Is Created.")
End Sub
End Class

此程序定义了类别Tree﹐它只含资料而无程序﹐为一「阳春型」之类别。当计算机执行到Form1_Click()程序内之宣告指令──
Dim a As New Tree()

就分配足够存放这 3项资料的内存空间给予对象 a。然而﹐此Tree类别只有资料而无程序。所以﹐对象 a无法接受外来之讯息。此时﹐可加入程序成员﹐使Tree类别含有程序、具有动力﹐对象就有能力来处理讯息了。例如﹕

'ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
End Sub
End Class
'------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click( ByVal sender As Object, ByVal
e As System.EventArgs)
Dim a As New Tree()
a.input(2.1)
Messagebox.Show("Set a.Height to 2.1", "Hello!")
End Sub
End Class

此程序输出:Set a.Height to 2.1
现在﹐Tree类别已拥有程序成员 input()。程序成员的写法与一般VB程序相同﹐只是它应宣告于类别内﹐成为类别之专属程序。此刻﹐对象 a含有 3项资料及 1个程序﹕

计算机执行到指令──
a.input(2.1)

就将讯息──input(2.1)传给对象 a。此时计算机呼叫并执行对象 a内之input() 程序。对象 a内之 input()就是定义于Tree类别内之input() ﹔于是Form1_Click()就把自变量──2.1 传给 input()内之 hei变量。



接下来﹐叙述──
height = hei

把 hei变量值存入对象 a之资料成员──height中。



此刻﹐对象 a对讯息之处理完成了﹐其内部资料改变了﹐亦即对象 a之内部状态(Internal State)改变了﹔这是对象的行为之一。上述您已经会加入一个程序了﹐依同样方法﹐继续加入其它程序﹐让对象的兴为更多采多姿。例如﹕

'ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-----------------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height A