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

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2546 更新时间:2009/4/23 19:01:10
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]  下一页


[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……
    咸宁网络警察报警平台