打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(5)程序多重定义
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3637  更新时间:2009/4/23 19:01:09  文章录入:mintao  责任编辑:mintao
bsp;            Sum = 127

当计算机执行到指令──  b.Add( 100, 27 ),由于有两个自变量﹐且型态皆为Integer﹔计算机就选上并执行第三个Add() 程序。此时计算机把100传给x﹐而27传给y。这多重定义之观念﹐也常用于建构者程序上。例如﹕

''''ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''---------------------------------------------------
Class Rectangle
    Private height As Integer, Width As Integer
    Public Overloads Sub New()
        height = 0
        width = 0
    End Sub
    Public Overloads Sub New(ByVal k As Integer)
        height = k
        width = k
    End Sub
    Public Overloads Sub New(ByVal h As Integer, ByVal w As Integer)
        height = h
        width = w
    End Sub
    Public Sub ShowArea()
        MessageBox.Show("Area = " + str(height * width))
    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 r1 As New Rectangle()
        Dim r2 As New Rectangle(8)
        Dim r3 As New Rectangle(5, 6)
        r1.ShowArea()
        r2.ShowArea()
        r3.ShowArea()
    End Sub
End Class

此程序输出﹕ Area = 0
             Area = 64
             Area = 30

宣告对象时﹐若未给予自变量值﹐计算机呼叫New()﹔若给一个自变量值── 8﹐就呼叫 New(ByVal k As Integer) ﹔若给二个自变量值──5 及 6﹐则呼叫New(ByVal h As Integer, ByVal w As Integer)。请再看一个例子:

''''ex04.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)
        height = h
        width = w
    End Sub
    Public Function Area() As Integer
        Area = height * width
    End Function
    Public Overloads Function CompareWith(ByVal a As Integer) As Integer
        Dim d As Integer
        d = Area() - a
        If d <> 0 Then
            CompareWith = 1
        Else
            CompareWith = 0
        End If
    End Function
    Public Overloads Function CompareWith(ByVal r As Rectangle) As Integer
        Dim d As Integer
        d = Area() - r.Area()
        If d <> 0 Then
            d = 1
        End If
        CompareWith = d
    End Function
    Public Overloads Function CompareWith( ByVal x As Rectangle, ByVal
                                       y As Rectangle) As Integer
        Dim d As Integer
        d = x.Area() - y.Area()
        If d <> 0 Then
            d = 1
        End If
        CompareWith = d
    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 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)
        height = h
        width = w
    End Sub
    Public Function Area() As Integer
        Area = height * width
    End Function
    Public Function CompareWithInteger(ByVal a As Integer) As Integer
        Dim d As Integer
        d = Area() - a
        If d <> 0 Then
&n

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

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