打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(12) 共享成员(Shared Member)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3258  更新时间:2009/4/23 19:01:10  文章录入:mintao  责任编辑:mintao
请记得,共享程序是类别对象(妈妈)的程序,妈妈与小孩各有隐私(封装),小孩呼叫妈妈程序(即共享程序)的格式为:

                 妈妈 . 共享程序()
                 例如:Employee.Average()

或               对象 . 共享程序()
                 例如:  e1.Average() 或 Me.Average()。

但妈妈呼叫小孩程序的格式为:

                 对象 . 一般程序()
                 例如:  e1.Average()。

因为妈妈程序里没有Me参考变量,所以上述的 k = GetSalary()指令相当于k = Me.GetSalary(),就错掉了。请再看一个例子:


''''ex08.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 p(10) As Employee
   
    Shared Sub New()
        counter = 0
    End Sub
    Public Sub New(ByVal na As String, ByVal s As Double)
        emp_name = na
        salary = s
        p(counter) = Me
        counter = counter + 1
    End Sub
    Shared Function GetObject(ByVal index As Integer) As Employee
        Dim sp As Employee
        If index > counter Or index < 0 Then
            sp = Nothing
        Else
            sp = p(index)
        End If
        GetObject = sp
    End Function
    Public Sub Display()
        MessageBox.Show("Name: " + emp_name + "    Salary: " + str(salary))
    End Sub
    Shared Sub Disp(ByVal e As Employee)
        e.Display()
    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)
  
        Dim e3 As New Employee("Linda", 15000)
        Dim e4 As New Employee("Alvin", 5000)
        Dim emp As Employee
        emp = Employee.GetObject(2)
        Employee.Disp( emp )
        emp = Employee.GetObject(1)
        Employee.Disp( emp )
    End Sub
End Class


此程序输出:
              Name: Linda   Salary: 15000
              Name: Lily    Salary: 20000

在妈妈对象里有一个p(10)数组,每次诞生对象时,就将新对象的参考值存入数组里。妈妈程序GetObject(i)则从数组中取出第i个对象,并传回来。妈妈程序使用格式:
                 对象 . 一般程序()

呼叫小孩的Display()程序。
    上述程序相当于:


 ''''ex09.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 p(10) As Employee
    Private Shared current As Employee
   
    Shared Sub New()
        counter = 0
    End Sub
    Public Sub New(ByVal na As String, ByVal s As Double)
        emp_name = na
        salary = s
        p(counter) = Me
        counter = counter + 1
    End Sub
    Shared Sub FindObject(ByVal index As Integer)
        Dim sp As Employee
        If index > counter Or index < 0 Then
            sp = Nothing
        Else
            sp = p(index)
        End If
        current = sp
    End Sub
    Shared Sub Disp()
        MessageBox.Show( "Name: " + current.emp_name +
                         "    Salary: " + str(current.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)
  
        Dim e3 As New Employee("Linda", 15000)
        Dim e4 As New Employee("Alvin", 5000)
        Employee.FindObject(2)
        Employee.Disp()
        Employee.FindObject(1)
        Employee.Disp()
    End Sub
End Class


此程序输出:
              Name: Linda   Salary: 15000
              Name: Lily    Salary: 20000


妈妈程序FindObject(i)则从数组中找到第i个对象,并记录在current变量里,不传回来。n

 

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

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