大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> VB.Net中文教程(1) 类别与封装性

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

时间: 2021-07-31 作者:daque

1. 类型的「步调分子」(procedure member) 类型 (class)之工作是把材料(data)和步调(procedure)构造并封装起来。类型报告计划机﹕「其东西应含有那些材料、应含有那些步调裨处置外界传来之消息」。类型须精细证明它的材料及步调﹐咱们称此材料是类型之「材料分子」(data member) ﹔而称此步调是类型之「步调分子」(procedure member)。相关类型实质之报告﹐即是所谓的类型设置(class definition)。类型设置之方法为──类型之用处为﹕颁布东西。比方﹕'ex01.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class tree public varity as string public age as integer public height as singleend 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.basimports system.componentmodelimports system.drawingimports 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 subend 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.input(2.1) messagebox.show("set a.height to 2.1", "hello!") end subend class此步调输入:set a.height to 2.1 此刻﹐tree类型已具有步调分子 input()。步调分子的写法与普遍vb步调沟通﹐不过它应颁布于类型内﹐变成类型之专属步调。现在﹐东西 a含有 3项材料及 1个步调﹕计划机实行到训令── a.input(2.1)就将消息──input(2.1)传给东西 a。此时计划机呼唤并实行东西 a内之input() 步调。东西 a内之 input()即是设置于tree类型内之input() ﹔所以form1_click()就把自变量──2.1 传给 input()内之 hei变量。接下来﹐报告── height = hei把 hei变量值惠存东西 a之材料分子──height中。现在﹐东西 a对消息之处置实行了﹐其里面材料变换了﹐亦即东西 a之里面状况(internal state)变换了﹔这是东西的动作之一。上述您仍旧会介入一个步调了﹐依同样本领﹐连接介入其它步调﹐让东西的兴为更多采多姿。比方﹕'ex03.basimports system.componentmodelimports system.drawingimports 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 functionend 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 subend class此步调输入如次﹕height = 2.1公尺 tree类型有2个步调分子──input() 和inquireheight()。类型之步调分子必需与其东西共同运用。方法为﹕亦即﹐必需以消息之情势展示。比方﹕ 即使步调分子不与东西相共同时﹐计划时机怎样处置呢﹖比方﹕'ex04.bas'some error here !imports system.componentmodelimports system.drawingimports 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 functionend 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 subend class当计划机看到form1_click()内之训令── h = inquireheight( )它视inquireheight()为一独力之步调﹐与tree类型内之inquireheight()无干﹔所以计划机去找此inquireheight()之设置﹐但找不着﹔以是步调错了。因之﹐您要控制个规则── 步调分子之独一工作是扶助东西之动作﹐必需与东西共同运用。2. 「封装性」观念 东西把材料及步调构造并「封装」(encapsulate) 起来﹐只透过一定的办法本领运用类型之材料分子和步调分子。东西犹如手提袋﹐只从恒定的启齿本领存取货色﹐要不您确定不敢把钱放在手提袋中。东西像一座「风火墙」养护类型中的材料﹐使其不受外界之感化。想一想我国的万里万里长城可养护关内的群众﹐制止受胡人侵吞﹐但万里长城并非实足封锁﹐而有山嘉峪关、玉门关等出进口。东西和万里万里长城之功效是普遍的﹐它养护其材料分子﹐但也有平常的材料存取弹道﹕以步调分子来存取材料分子。请看个步调﹕'ex05.basimports system.componentmodelimports system.drawingimports system.winforms'-------------------------------------------------------class tree public varity as string public age as integer public height as singleend 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 messagebox.show("height = " + str(a.height) + "公尺") end subend class此步调输入如次﹕height = 2.1公尺 此步调中﹐tree类型含有 3个材料分子﹐即东西内含有3个材料值,此类型之步调分子能径直存取之。同声,也承诺其它步调来存取材料分子之值﹐其存取方法为﹕比方﹕ a.height = 2.1此训令把 2.1惠存东西 a之height变量中。所以东西 a之实质为﹕ 请看个罕见缺点如次﹕'ex06.bas'some error here!imports system.componentmodelimports system.drawingimports system.winforms'-------------------- ------------------------------------class tree public varity as string public age as integer public height as singleend 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() height = 2.1 messagebox.show("height = " + str(a.height) + "公尺") end subend classform1_click()步调内之训令── height = 2.1,此height变量并未与东西共同运用﹐计划机不觉得它是tree类型之height变量。计划机视其为form1_click()之机动变量(automatic variable)﹐但却未见到它的颁布﹐因之步调错了﹗这是东西对其材料分子养护最松的景象﹐由于东西分属类型(即tree)除外的步调(如form1_click()步调)尚能存取材料分子的实质。就像一颗空包弹﹐除去引信管外﹐尚有很多弹道可使空包弹内之化学方剂爆裂﹔您将不敢把空包弹摆在铁鸟上﹐因何时会爆裂将没辙遏制。同理﹐tree类型之材料──height变量﹐连外部的form1_click()皆可随便变换它﹔那么有一天height之实质出题目了﹐将难以检查堕落之来由﹐这种步调将让您大伤思想﹐由于您已没辙控制情景了。 此刻的vb步调中﹐能采用较精细之养护办法﹐使您较能遏制类型内材料的变革情景。比方﹐'ex07.bas'some error here!imports system.componentmodelimports system.drawingimports system.winforms'-----------------------------------------class tree private varity as string private age as integer private height as singleend 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 public sub form1_click( byval sender as object, byval e as system.eventargs) dim a as new tree() a.height = 2.1 messagebox.show("height = " + str(a.height)) end subend class此步调将从来的public专用字改为private﹐对tree类型之材料分子采用庄重之养护办法。public 与private之辨别为── public 表白此类型外之步调可来存取材料分子。 private 表白此类型外之步调绝没辙径直存取材料分子﹐惟有步调分子本领存取材料分子。以是﹐计划机看到训令── a.height = 2.1,因tree类型采用庄重养护办法(private)﹐则form1_click()步调不许运用height变量称呼。以是训令── a.height = 2.1错了。大概您问及﹕如许岂不是没辙存取类型内之材料分子吗﹖谜底是﹕「类型内之步调分子(member function) 可存取材料分子﹐而类型外之步调能藉步调分子代之存取材料分子。」 图1、类型之勾通弹道──步调分子 这犹如﹐引信管本领惹起空包弹内之化学方剂爆裂﹐人们只能经过引信管引爆之﹐让人们感触运用空包弹既安定又大略。同样地﹐东西经过步调分子和外界勾通﹐可缩小外界偶尔中妨害东西内之材料(偶尔中引爆空包弹)。比方﹕'ex08.basimports system.componentmodelimports system.drawingimports system.winforms'--------------------------------------------------class tree private varity as string private age as integer private height as single public sub input(byval hei as single) height = hei end subend 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.input(2.1) messagebox.show("ok") end subend class 将input()摆在tree类型中﹐为tree之步调分子﹐它能存取材料分子height之值。把input()步调颁布为public表白类型外之步调可藉来呼唤它﹐其呼唤方法为── 大略准则是﹕ public 表白受权给外界之步调藉由此方法呼唤步调分子。即使此步调改写为﹕'ex09.bas'some error here!imports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------class tree private varity as string private age as integer private height as single private sub input(byval hei as single) height = hei end subend 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.input(2.1) messagebox("ok") end subend class这步调有题目﹐由于 input()是tree类型之private步调分子而非public步调分子﹐以是不许运用如次方法──以是此步调错了。 请再看个例子吧﹗'ex10.basimports system.componentmodelimports system.drawingimports system.winforms'-------------------------------------------class tree private varity as string private height as single public age as integer public sub showage() messagebox.show("age = " + str(age)) end subend 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.age = 8 a.age = a.age + 2 a.showage() end subend classtree类型包括 2个private分子── variety及height﹐且有 2个public分子── age及 showage()。因为age是public材料分子﹐以是fom1_click()可运用方法──来存取tree内之age变量。训令── a.age = 8把8惠存东西 a内之age 变量。训令──a.age = a.age + 2使东西a之age变量值加上2﹐变成10。因为showage()步调是public步调分子﹐也可运用方法──来呼唤 showage()步调。因为类型(即东西)之手段是养护材料﹐而且供给步调分子来与外界勾通(接收、处置、并反馈消息)。常常﹐材料分子皆颁布为private﹐而步调分子皆颁布为public。亦即尽管少用方法──而尽管多用方法──比方﹕'ex11.basimports system.componentmodelimports system.drawingimports system.winforms'--------------------------------------------------------------------class tree private varity as string private age as integer private height as single public sub input(byval v as string, byval a as integer, byval hei as single) varity = v age = a height = hei end sub public sub show() messagebox.show(varity + ", " + str(age) + ", " + str(height)) end subend 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, b as new tree() a.input("peach", 8, 2.1) b.input("pinapple", 2, 0.5) a.show() b.show() end subend class 这个vb步调﹐tree内之材料分子──variety, age及height皆为private分子,而input()及show()步调是public分子。form1_click()步调中﹐开始出生东西── a及 b。接下来﹐训令──把 3项材料辨别惠存东西 a之材料分子中﹐a 之实质为﹕同样地﹐训令── b.input( "pineapple", 2, 0.5 )也把 3项材料惠存东西 b之中﹐则 b之实质为﹕结果﹐呼唤show()步调把东西 a和东西 b之实质表露出来﹕ peach, 8, 2.1 pineapple, 2, .5n

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.