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

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

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

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

1. 类别的「程序成员」(Procedure Member) 类别 (Class)之任务是把资料(Data)和程序(Procedure)组织并封装起来。类别告诉计算机﹕「其对象应含有那些资料、应含有

 

 

1. 类别的「程序成员」(Procedure Member)

 

   类别 (Class)之任务是把资料(Data)和程序(Procedure)组织并封装起来。类别告诉计算机﹕「其对象应含有那些资料、应含有那些程序裨处理外界传来之讯息」。类别须详细说明它的资料及程序﹐我们称此资料是类别之「资料成员」(Data Member) ﹔而称此程序是类别之「程序成员」(Procedure Member)。有关类别内容之叙述﹐就是所谓的类别定义(Class Definition)。类别定义之格式为──

       

类别之用途为﹕宣告对象。例如﹕

 

ex01.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

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

Class Tree

   Public varity As String

   Public age As Integer

   Public height As Single

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 Tree()

       MsgBox("Object a Is Created.")

   End Sub

End Class

 

此程序定义了类别Tree﹐它只含资料而无程序﹐为一「阳春型」之类别。当计算机执行到Form1_Click()程序内之宣告指令──

                     Dim a As New Tree()

 

就分配足够存放这 3项资料的内存空间给予对象 a。然而﹐此Tree类别只有资料而无程序。所以﹐对象 a无法接受外来之讯息。此时﹐可加入程序成员﹐使Tree类别含有程序、具有动力﹐对象就有能力来处理讯息了。例如﹕

 

ex02.bas

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

End Class

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

Public Class Form1

Tags:

文章评论


<