d 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 x As New Club("sogo") Dim a As New Person("Alvin", 32) Dim b As New Person("Judy", 28) x.join(a) x.join(b) x.display() End Sub End Class
此程序输出﹕ Club: sogo has member: Alvin, 32 Judy, 28
c_name 指向Strclass之对象﹐这对象内含俱乐部之名称。pa指向ArrayList之对象﹐这对象可包含许多会员(Person)对象。join()程序将Person之对象存入pa所指的ArrayList对象中。 Club之对象含ArrayList之对象﹐此集合对象(Collections)含有Person之对象﹐表达了「集合/成员」关系。例如﹐x 对象内含a 和b 对象。
此图表示﹕"sogo"俱乐部共有"Alvin" 和"Judy"两个会员﹐亦即x 是「集合」﹐而a 和b 是「成员」(Member)。 值得注意﹕这软件是利用已有类别──Strclass及Integer组合成应用类别──Person。再利用Person类别及ArrayList 类别组合成更复杂之应用类别──Club。未来﹐可利用Club及其它类别构筑更大的应用类别﹐依此类推﹐便能创造庞大又可靠的软件了。例如:
''''ex06.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms Imports System.Collections ''''---------------------------------------------------- Class Person Private p_name As String Private p_age As Integer Public Sub New(ByVal na As String, ByVal a As Integer) p_name = na p_age = a End Sub Public Sub display() Messagebox.Show(Me.p_name + ", " + str(Me.p_age)) End Sub End Class
Class Club Private c_name As String Private pa As ArrayList Public Sub New(ByVal na As String) c_name = na pa = New ArrayList() End Sub Public Sub join(ByVal p As Person) pa.Add(p) End Sub Public Sub display() Messagebox.Show("Club: " + Me.c_name + " has member:") Dim p As Person For Each p In pa p.display() Next 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 x(2) As Club x(0) = New Club("sogo") x(1) = New Club("gold") Dim a As New Person("Alvin", 32) Dim b As New Person("Judy", 28) Dim c As New Person("Bob", 38) x(0).join(a) x(0).join(b) x(1).join(b) x(1).join(c) x(0).display() x(1).display() End Sub End Class
此程序输出:
Club: sogo has member: Alvin, 32 Judy, 28 Club: gold has member: Judy, 28 Bob, 38
组合对象x 含"sogo"及"gold"两俱乐部﹐其中"gold"俱乐部拥有两个会员──"Alvin" 及"Judy"﹐而"sogo"俱乐部拥有两位会员──"Judy"及"Bob" 。x(0)代表"sogo"俱乐部﹐s(1)代表"gold"俱乐部﹐所以指令── s(0).join( a ) 表示a 加入"gold"俱乐部﹐成为其会员。n
上一页 [1] [2] [3] |