s = x y
End Sub
Public Sub Show()
MessageBox.Show("Sum = " str(s))
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 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
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
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




