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

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

 

 

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 a As New Sum()

       a.Add()

       a.Show()

       a.Add(80)

       a.Show()

       Dim b As New Sum()

       b.Add(100, 27)

       b.Show()

   End Sub

End Class

 

此程序输出如下﹕   Sum = 8

                  Sum = 85

                  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

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