nbsp; e2.Display() End Sub End Class
此程序输出如下﹕ Name:Tom Salary:25000 Name:Lily Salary:20000
指令── salary = emp.salary + money 已经在Employee类别的保护中,这是不错的做法。 另一种方法是采用共享资料成员,是同一类别里各对象皆共享其值;一般资料成员的值是封装于各对象内,别的对象是拿不到的。共享资料成员的值是在对象之外,但封装在类别内,只要是该类别的对象皆能拿到该值。因为一般资料成员的值封装于对象内,所以又称为对象变量(Instance Variable);共享资料成员封装于类别,所以又称为类别变量(Class Variable)。上述程序可改用共享变量如下﹕
''''ex04.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms ''''---------------------------------------------------- Class Employee Private emp_name As String Private salary As Double Private Shared temp As Double
Public Overloads Sub New(ByVal na As String) emp_name = na End Sub Shared Sub New() temp = 0 End Sub Public Overloads Sub New(ByVal na As String, ByVal s As Double) emp_name = na salary = s End Sub Public Sub output() temp = salary End Sub Public Sub input() salary = temp End Sub Public Sub add_temp(ByVal money As Double) temp = temp + money End Sub Public Sub Display() MessageBox.Show("Name: " + emp_name + " Salary: " + str(salary)) 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 e1 As New Employee("Tom") Dim e2 As New Employee("Lily", 20000) e2.output() e2.add_temp(5000) e1.input() e1.Display() e2.Display() End Sub End Class
此程序输出如下﹕ Name:Tom Salary:25000 Name:Lily Salary:20000
宣告指令── Private Shared temp As Double 说明了﹐temp是共享资料成员。兹归纳,共享资料成员与一般资料成员之区别为:
「共享资料成员是各对象公用的资料﹐ 而一般资料成员是对象之私有资料。」
当第1次诞生对象时﹐共享资料成员就由Shared Sub New()诞生了。当执行到指令── Dim e1 As New Employee("Tom")
就诞生对象e1。但temp比e1早诞生。接下来﹐执行指令── Employee e2( "Lily", 20000 )
它诞生对象e2。此时Employee类别之内容为﹕
图3、类别沟通方式──使用共享资料成员
其中﹐e1及e2各含有一份emp_name及salary资料。然而整个Employee类别才只有一份temp资料﹐对象共同分享此temp内之资料。Employee类别里的任何对象皆可视temp为其资料成员来使用﹐但事实上只有一个temp。由于它属于所有的对象﹐所以各对象皆可经由程序成员存取temp资料。重要的是﹐temp已在Employee类别的保护之中﹐藉由它传递资料就无受外界干扰之虞了。 共享资料成员除了供对象之间的沟通之用外﹐还有其重要的用途──记载类别的状况﹐例如记录该类别共诞生了多少对象。请看个例子﹕
''''ex05.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms ''''----------------------------------------------------------- Class Employee Private emp_name As String Private salary As Double Private Shared counter As Integer Private Shared sum As Double Shared Sub New() counter = 0 sum = 0 End Sub Public Sub New(ByVal na As String, ByVal s As Double) emp_name = na salary = s counter = counter + 1 sum = sum + salary End Sub Public Sub Display_Avg() MessageBox.Show("The number of employee : " + str(counter)) MessageBox.Show("Average salary : " + str(sum / counter)) End Sub Public Sub Display() MessageBox.Show("Name: " + emp_name + " Salary: " + str(salary)) 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 e1 As New Employee("Tom", 25000) Dim e2 As New Employee("Lily", 20000) e1.Display() e2.Display() e1.Display_Avg() End Sub End Class
此程序输出﹕ Name: Tom Salary: 25000 Name: Lily Salary: 20000 The number of employee : 2 Average salary : 22500
counter 记录类别中含有多少对象。sum 储存各对象的salary值之总和。于此﹐必须给予 counter及 sum初值。共享变量之初值设定格式为──
Shared Sub New() 共享资料成员1 = 初值 共享资料成员2 = 初值 ...... End Sub
计算机开始执行,诞生第1个对象时﹐各共享成员就诞生了﹐而且设定了初值。此时Employee类别的内容为﹕
对象诞生时﹐会去执行建构者程序──Public Sub New(...),先把各资料存入对象之私有成员中﹔同时也执行指令──counter = counter + 1﹐使共享变量 counter值加上 1。此外﹐也执行指令──sum = sum + salary ﹐把e1之salary值加到 sum里头。此时共享变量的内容为﹕
counter 值为 1﹐表示Employee内已诞生一个对象。接着﹐诞生个体e2﹐计算机又执行建构者程序──Public Sub New(...)。它把资料存入对象e2中﹐使 counter加上 1﹐也把e2之salary值加到 sum中。此时﹐共享变量之内容为﹕
注意﹐e1含有 4个资料成员──emp_name、salary、counter 及sum ﹔其值分别为 "Tom"、 上一页 [1] [2] [3] [4] 下一页 [ORACLE]Oracle内存结构:Shared Pool的详细信息
|