VB.Net中文教程(1) 类别与封装性(2)

页面导航:首页 > 软件编程 > vb.net > VB.Net中文教程(1) 类别与封装性(2)

VB.Net中文教程(1) 类别与封装性(2)

来源: 作者: 时间:2016-02-05 12:32 【

End FunctionEnd Class------------------------------------------------------------------------Public Class Form1 Inherits System.WinForms.Form 

 

 

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

       Dim h As Single

       a.input(2.1)

       h = a.inquireHeight()

       Messagebox.Show("height = " + str(h) + "公尺", "HI!")

   End Sub

End Class

 

此程序输出如下﹕height = 2.1公尺

   Tree类别有2个程序成员──input() 和inquireHeight()。类别之程序成员必须与其对象配合使用。格式为﹕

                   

亦即﹐必须以讯息之形式出现。例如﹕

                         

如果程序成员不与对象相配合时﹐计算机会如何处理呢﹖例如﹕

 

ex04.bas

Some Error Here !

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

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

Class Tree

   Public varity As String

   Public age As Integer

   Public height As Single

   Public Sub input(ByVal hei As Single)

       height = hei

   End Sub

   Public Function inquireHeight() As Single

       inquireHeight = height

   End Function

End Class

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

Public Class Form1

   Inherits System.WinForms.Form

  

   Public Sub New()

       MyBase.New()

Tags:

文章评论


<