打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(7) Me参考值
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2651  更新时间:2009/4/23 19:01:09  文章录入:mintao  责任编辑:mintao
400元﹐而非原来的100元了。此orange再接受讯息── add(80)﹐其 balance值增加为480 元。orange接到第 2个讯息── add(80)时﹐计算机再执行add() 程序﹐其再度传回orange的参考值﹐使得整个指令──
         
又成为orange之别名。因之﹐亦能把disp()讯息接于其后﹐如下﹕

''''ex04.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''------------------------------------------------------------------------------------
Public Class Money
    Private balance As Decimal
    Public Sub New(ByVal amount As Decimal)
        balance = amount
    End Sub
    Public Function add(ByVal saving As Decimal) As Money
        balance = balance + saving
        add = Me
    End Function
    Public Sub Display()
        MessageBox.Show("Balance is " + str(balance))
    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 orange As New Money(100)
        orange.add(300).add(80).Display()
    End Sub
End Class

此程序输出如下﹕Balance is 480
   orange对象接到第 1个讯息──add(300)﹐计算机就执行add()程序,执行到结尾指令﹐传回Me(即orange对象)参考值。此时Form1_Click()的orange.add(300)就是orange对象之参考值﹐亦即orange.add() 是orange对象之别名﹔则orange和 orange.add(300)重合在一起﹐代表着同一对象──原来的orange对象。

          

     接下来﹐第 2个讯息──add(80)传给orange.add(300) ﹐相当于传给orange对象。再度执行到 add()里的的add = Me指令时﹐又令orange.add(300).add(80) 成为 orange.add(300)之别名﹐即orange之别名﹔于是﹐三者代表同一对象──原来的orange对象。
          
         

     接下来﹐第3个讯息──Display传给orange.add(300).add(80) ﹐相当于传给orange对象。
         
于是输出orange对象内的balance值。
    以程序传回Me参考值之技巧将应用于许多方面。为了更了解这种方法﹐请看个特殊情形── 程序传回新对象之参考值。此对象不是目前对象,但内容是从目前对象拷贝而来。这不同于传回Me参考值的情形﹐两种用法常令人搞混﹗现在﹐把程序改为──

''''ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''------------------------------------------------------------------------------------
Public Class Money
    Private balance As Decimal
    Public Sub New(ByVal amount As Decimal)
        balance = amount
    End Sub
    Public Function add(ByVal saving As Decimal) As Money
        Dim newobj As Money
        newobj = New Money( balance + saving )
        add = newobj
    End Function
    Public Sub Display()
        MessageBox.Show("Balance is " + str(balance))
    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 orange As New Money(100)
        orange.add(300).add(80).Display()
    End Sub
End Class

此程序输出如下﹕Balance is 480

     当orange对象接到第 1个讯息──add(300)﹐计算机就执行add()程序﹐诞生一个Money类别的新对象﹐把目前对象内容(即orange对象之值)拷贝一份给Form1_Click()。这份拷贝就是orange.add(300)之值。
       
    orange.add(300) 即为拷贝回来的那份对象﹐并非原来的orange对象。当讯息──add(80)传给orange.add(300)所代表的对象时﹐计算机就执行add()函数﹐此时目前对象是orange.add(300) 而非原来的orange。执行时﹐又把目前对象──orange.add(300)内容拷贝一份给新诞生的对象,传回给Form1_Click()程序﹐这份拷贝就是orange.add(300).add(80) 之值。

 

    由于每回执行add()就产生一份新对象(虽然内容相同﹐但占不同的内存空间)﹐其后的讯息皆传给add()所诞生之新对象﹐而非orange对象,所以这些讯息皆不影响原来orange对象之内容。
     请注意﹕Display()并未传回对象之参考值﹐则Display()讯息之后不得再接其它讯息了。所以﹐如果Form1_Click()程序改写如下,那就错了──

Protected Sub Form1_Click( ByVal sender As Object,
                            ByVal e As System.EventArgs)
        Dim orange As New Money(100)
        orange.add(300).Display().add(80)    ''''Error!
    End Sub
End Class

因Display()不传回对象之参考值﹐则指令──
                
其后之讯息──add(80) 是错的。如何改正呢﹖很简单﹐只需叫Display()程序传回 Me(目前对象之参考值)或新对象之参考值即可﹐如下﹕

''''ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''-------------------------------------------------------------------------------------
Public Class Money
    Private balance As Decimal
    Public Sub New(ByVal amount As Decimal)
        balance = amount
    End Sub
    Public Function add(ByVal saving As Decimal) As Money
        Dim newobj As Money
        newobj = New Money( balance + saving )
        add = newobj
    End Function
    Public Function Display() As Money
        MessageBox.Show("Balance is " + str(balance))
        Display = Me
    End Function
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 orange As New Money(100)
        orange.Display().add(300).Display().add(80).Display()
    End Sub
End Class

此程序输出﹕
       &

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

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