打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(12) 共享成员(Shared Member)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3258  更新时间:2009/4/23 19:01:10  文章录入:mintao  责任编辑:mintao
25000、 2及 45000。同时e2含有4 个资料成员──emp_name、salary、counter 及 sum﹔其值分别为"Lily"、20000 、2 及 45000。只是e1之 counter值等于e2之 counter值﹐同时e1之sum 值等于e2之 sum值。

 


2. 共享程序成员
    刚才介绍过共享资料成员(Shared Data Member)之观念。共享资料成员是各对象的公用数据成员﹐但并不属于任何对象﹐而是属于类别的。除了资料成员之外,也有共享程序成员,它是属于类别的﹐而并不属于任何对象﹐其不是用来存取某对象内的值﹐而只储存取共享资料成员之值。任何一般程序成员皆可呼叫共享程序成员来存取共享成员之值。例如﹕

''''ex06.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
    Shared Function NumberOfObject() As Integer
        NumberOfObject = counter
    End Function
    Shared Function Average() As Double
        Average = sum / counter
    End Function
    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)
        MessageBox.Show(str(e1.NumberOfObject()) + " objects")
        MessageBox.Show("Avgerage: " + str(e2.Average()))
        Dim e3 As New Employee("Linda", 15000)
        MessageBox.Show(str(e3.NumberOfObject()) + " objects")
        MessageBox.Show("Average: " + str(e3.Average()))
    End Sub
End Class

此程序输出:

             2 objects
             Average: 22500
             3 objects
             Average: 20000

还记得吗﹖一般程序成员是用来处理对象内的资料﹐所以呼叫一般程序成员之格式为──
            对象. 一般程序成员

呼叫这程序成员的目的是来处理此对象之内容。共享程序成员之目的并非在于处理对象之内容﹐而是存取共享资料成员之值或处理些有关于整个类别的事情。因之﹐呼叫共享程序成员的格式为──

                类别 . 共享程序成员

呼叫这共享程序成员之目的是来处理此类别之资料或有关之事情。
一种格式──
                对象 . 共享程序成员

每个对象皆来自某个类别﹐VB看到对象名称时﹐可对应到它的类别﹐所
以VB在编译这种格式时﹐会将之转换成为﹕

              类别 . 共享程序成员

亦即﹐指令──  c1.NameOfObject()
      及        c2.NameOfObject()

皆会转换成为-- Employee.NameOfObject()

同理﹐e1.Average()也就相当于Employee.Average() 了。
    由上述可知共享程序成员并非处理某个特定的对象值﹐所以没有必要去呼叫一般的程序成员。VB也就禁止共享程序成员呼叫一般的程序成员。不过﹐反之一般程序成员却可以呼叫共享程序成员﹐以便必要时可藉由共享程序成员来取得有关于整个类别的资料。例如:

''''ex07.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
        Me.sum = Employee.sum + salary
    End Sub
    Public Function GetSalary() As Double
        GetSalary = salary
    End Function
    Shared Function NumberOfObject() As Integer
        NumberOfObject = counter
    End Function
    Shared Function Average() As Double
        Dim k As Double
        Average = Employee.sum / Employee.counter
        '''' salary = salary + 2000    '''' Error!
        '''' k = GetSalary()         '''' Error!
    End Function
    Public Sub Disp()
        MessageBox.Show(str(Employee.NumberOfObject()) + " objects")
        MessageBox.Show("Avgerage: " + str(Employee.Average()))
    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)
        e2.disp()
        Dim e3 As New Employee("Linda", 15000)
        e3.disp()
   End Sub
End Class

此程序输出:
             2 objects
             Average: 22500
             3 objects
             Average: 20000

 

 

 

 

共享程序,上述Disp()是一般程序成员,此程序内的指令:

     Public Sub Disp()
        MessageBox.Show(str(Employee.NumberOfObject()) + " objects")
        ......
     End Sub

这就呼叫了共享的NumberOfObject()程序。
共享程序不可呼叫一般程序,上述Average()是共享程序,它不能呼叫一般程序,所以Average()内的指令有错:

 Shared Function Average() As Double
        Dim k As Double
        Average = Employee.sum / Employee.counter
        '''' salary = salary + 2000    '''' Error!
        '''' k = GetSalary()         '''' Error!
    End Function
   

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口