转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.Net中文教程(5)程序多重定义         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3635 更新时间:2009/4/23 19:01:09
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]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台