繁体中文
设为首页
加入收藏
当前位置:在线教程首页 >> 程序设计 >> .Net专区 >> VB.net语言 >> VB.Net中文教程(1) 类别与封装性(2)

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

2005-10-10 05:51:00  作者:未知  来源:教程网  浏览次数:242  文字大小:【】【】【

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

 

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

       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 = inquireHeight()

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

   End Sub

End Class

 

当计算机看到Form1_Click()内之指令──

               h = inquireHeight( )

 

它视inquireHeight()为一独立之程序﹐与Tree类别内之inquireHeight()无关﹔于是计算机去找此inquireHeight()之定义﹐但找不着﹔所以程序错了。因之﹐您要掌握个原则── 程序成员之唯一任务是支持对象之行为﹐必须与对象配合使用。

2. 「封装性」概念

 

   对象把资料及程序组织并「封装」(Encapsulate) 起来﹐只透过特定的方式才能使用类别之资料成员和程序成员。对象如同手提袋﹐只从固定的开口才能存取东西﹐否则您一定不敢把钱放在手提袋中。对象像一座「防火墙」保护类别中的资料﹐使其不受外界之影响。想一想我国的万里长城可保护关内的人民﹐避免受胡人侵犯﹐但长城并非完全封闭﹐而有山海关、玉门关等出入口。对象和万里长城之功能是一致的﹐它保护其资料成员﹐但也有正常的资料存取管道﹕以程序成员来存取资料成员。请看个程序﹕

 

"ex05.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()

       a.height = 2.1

 

 

此文章不能满足您的需求?那就就Google一下吧:)
Google
 

责任编辑:admin


相关文章
SA空口令破解和保护
怎样使MySQL更安全?
Mysql数据库的安全配置、实用技巧
如何安全的配置和应用MySQL数据库?
Oracle数据库安全策略分析
SQL Server 2000的安全配置
使一个新的MySQL安装更安全
教您如何安全的应用MySQL
MySQL数据库安全配置
MySQL安全性指南(1)
MySQL安全性指南(2)
 

最新文章

更多

· 类别与封装性(1)
· 类别与封装性(2)
· 类别与封装性(3)
· 类别与封装性(4)
· 类别与封装性(5)
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(3)程序多...

推荐文章

更多

· 类别与封装性(1)
· 类别与封装性(2)
· 类别与封装性(3)
· 类别与封装性(4)
· 类别与封装性(5)
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(3)程序多...

热点文章

更多