当前位置:编程学习 > VB >>

VB.Net中文教程(3)程序多重定义(3)

 

 

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 r1 As New Rectangle(10, 50)

       Dim r2 As New Rectangle(20, 25)

       If r1.CompareWith(400) = 0 Then

           MessageBox.Show("EQUAL")

       Else

           MessageBox.Show("NOT EQUAL")

       End If

       If r1.CompareWith(r2) = 0 Then

           MessageBox.Show("EQUAL")

       Else

           MessageBox.Show("NOT EQUAL")

       End If

       If r1.CompareWith(r1, r2) = 0 Then

           MessageBox.Show("EQUAL")

       Else

           MessageBox.Show("NOT EQUAL")

       End If

   End Sub

End Class

 

此程序输出﹕ NOT EQUAL

              EQUAL

              EQUAL

 

如此﹐CompareWith()程序就有三种用途了﹔如果您想增加其它用途﹐可尽情地再定义它。r1.CompareWith(400)呼叫第1个CompareWith(),比比看r1面积是否大于400;r1.ComapreWith(r2) 呼叫第2个CompareWith(),比比看r1面积是否大于r2的面积;r1.ComapreWith(r1, r2) 比比看r1面积是否大于r2的面积。如果没有使用多重定义方法,这三个程序名称不能相同。例如﹕上述程序可改写为──

 

ex05.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

-------------------------------------------------------------------------------------------

Class Rectangle

   Private height As Integer, Width As Integer

   Public Sub New(ByVal h As Integer, ByVal w As Integer)

补充:软件开发 , Vb ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,