转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.Net中文教程(12) 共享成员(Shared Member)         ★★★★

VB.Net中文教程(12) 共享成员(Shared Member)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2539 更新时间:2009/4/23 19:01:10
请记得,共享程序是类别对象(妈妈)的程序,妈妈与小孩各有隐私(封装),小孩呼叫妈妈程序(即共享程序)的格式为:

                 妈妈 . 共享程序()
                 例如: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] 


[ORACLE]Oracle内存结构:Shared Pool的详细信息  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台