大雀软件园

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

VB.Net中文教程(3) 继承与封装性

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

1. 接受与封装性(encapsulation)1.1 公用与独占数据 前方已引见「封藏性」(encapsulation) 之看法。即是﹕类型内所设置之材料分子﹐限于于步调分子本领存取之。此刻所面对之题目为﹕子类型是否径直存取父类型之材料呢﹖就犹如﹕后代从双亲亲接受了财富﹐但是否取用或卖出接受而来的财富呢﹖即使不妨﹐明显违反了「封藏性」之理念。即使不行﹐明显带给步调员莫斯科大学之控制。表面上﹐百分之百的封藏性最为完备﹔但运用上﹐若赋予子类型几何款待﹐能普及步调之弹性及功效。为领会决此鱼与熊掌不行一举多得之窘境﹐vb供给三种采用﹕ (1) public ──指定某些数据为公用的﹐任何步调皆可径直取用之﹔此时并无任何封藏效率。 (2) protected ──指定某些材料为家属公用﹐亦即惟有后代类型内之步调可取用﹐非后代类型之步调必需呼唤家属内之步调代为存取。 (3) private ──指定某材料为类型独占﹐限于于该类型之步调才可取用。子类型之步调也必需呼唤父类型之步调代为存取﹐此时具百分之百封藏性。 先前引见「封装性」基础观念时,您仍旧看法了public和private的蓄意了,至于protected则共同接受来运用,所以在此更加夸大它。 因为vb向实际协调﹐开了简单之门﹐无百分之百封藏性﹔以是有些人觉得 vb并非完备的 oop谈话。您觉得怎样呢﹖请看个步调﹕'ex01.basimports system.componentmodelimports system.drawingimports system.winforms'---------------------------------------------------------class person private name as string public age as integer public sub new(byval na as string, byval a as integer) name = na age = a end subend classclass teacher inherits person public salary as single public sub new(byval na as string, byval a as integer, byval sa as single)mybase.new(na, a) salary = sa end sub public sub modifyage(byval a as integer) age = a 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 crag as new teacher("crag", 38, 45000) crag.modifyage(42) messagebox.show( "age: " + str(crag.age) + ", salary: " + str(crag.salary)) end subend class此步调输入如次﹕ age: 42 salary: 45000 person之age材料分子设置为 public﹐表白 age为公用数据﹐任何步调皆可存取之。因之﹐teacher 之 modifyage()可运用age这称呼。对立地﹐于 teacher类型中﹐就不得运用name这称呼﹐由于name设置为privatec﹐表白name为person类型之独占材料。再看teacher类型之salary材料﹐它设置为public﹐表白公用之意。以是form1_click()可径直运用 crag.salary方法博得salary之值。但是﹐不许写成﹕crag.name ﹐由于name为独占材料。比方,下述步调是不对的:'ex02.bas'some error here!imports system.componentmodelimports system.drawingimports system.winforms'---------------------------------------------------------class person private name as string public age as integer public sub new(byval na as string, byval a as integer) name = na age = a end subend classclass teacher inherits person public salary as single public sub new(byval na as string, byval a as integer, byval sa as single) mybase.new(na, a) salary = sa end sub public sub modifyname(byval na as string) name = na 'error here! 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 crag as new teacher("crag", 38, 45000) crag.modifyname("crag clinton") messagebox.show( "age: " + str(crag.age) + ", salary: " + str(crag.salary)) end subend class由于name是person类型的独占材料,在子类型teacher的modifyname()里不许运用此材料称呼,以是错了。即使将person类型设置为﹕'ex03.basimports system.componentmodelimports system.drawingimports system.winforms'---------------------------------------------------------class person protected name as string public age as integer public sub new(byval na as string, byval a as integer) name = na age = a end sub public function getname() as string getname = name end functionend classclass teacher inherits person public salary as single public sub new(byval na as string, byval a as integer, byval sa as single) mybase.new(na, a) salary = sa end sub public sub modifyname(byval na as string) name = na 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 crag as new teacher("crag", 38, 45000) crag.modifyname("crag clinton") messagebox.show( "age: " + str(crag.age) + ", name: " + crag.getname()) end subend class此步调输入: age: 42, name: crag clinton 此时﹐name为家属公用之材料﹐但凡person之后代类型皆可取用之。但家属外之步调(如 form1_click()步调)仍不得径直运用之。即使上述设置改为﹕class person protected name as string private salary as decimal public age as integer public sub new(byval na as string, byval a as integer) name = na age = a end subend class此时﹐salary为类型独占﹐其它类型不得运用。name为家属独占﹐家属外之类型不得运用。age为公用﹐任何类型皆可用。 1.2 公用与独占步调 上节引见过﹕材料分子有 private、protected 及public之分。同样地﹐步调分子也可分为 private、protected 及public。固然在运用上﹐步调分子大多设置为public﹐但需要时﹐也能将进程设置为private 或 protected。独占步调和独占材料一律﹐限于于该类型之步调本领呼唤它。比方﹕'ex04.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------------------------class person private name as string private age as integer private sub modifyage(byval a as integer) age = a end sub public sub new(byval na as string, byval a as integer) name = na age = a end sub public sub modify(byval na as string, byval a as integer) name = na modifyage(a) end sub public sub show() messagebox.show("name: " + name + " 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 p1 as new person("david", 25) p1.show() p1.modify("david smith", 28) p1.show() end subend class此步调输入: name: david age: 25 name: david smith age: 45 modifyage()为独占步调﹐new()及modify()为公用步调。modifyage()为person类型之步调分子﹐以是modify()能呼唤modifyage()。person类型外之因变量不许呼唤modifyage()。比方﹕在form1_click()中﹐可写着 p1.modify()﹐由于modify()为公用步调。但于form1_click()内﹐就不行写着 p1.modifyage()﹐由于modifyage()为 private因变量。即使放宽对modifyage()之控制﹐使person之子类型能呼唤modifyage()﹐就须设置modifyage()为 protected因变量﹐如次﹕'ex05.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------------------------class person protected name as string private age as integer protected sub modifyage(byval a as integer) age = a end sub public sub new(byval na as string, byval a as integer) name = na age = a end sub public sub show() messagebox.show("name: " + name + " age: " + str(age)) end subend classclass teacher inherits person public sub new(byval na as string, byval a as integer) mybase.new(na, a) end sub public sub modify(byval na as string, byval a as integer) mybase.name = na mybase.modifyage(a) 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 p1 as new teacher("david", 25) p1.show() p1.modify("kent smith", 45) p1.show() end subend class此步调输入: name: david age: 25 name: kent smith age: 45此时﹐后代类型之因变量能呼唤modifyage()﹐但家属外之类型仍不行呼唤它。归纳归结为大略准则﹕ ◎即使承诺任何类型运用﹐就颁布为public。 ◎即使承诺子类型运用﹐就颁布为protected 。 ◎即使承诺本类型运用﹐就颁布为private 。 请在看个例子:'ex06.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------------------------class person protected name as string private age as integer protected sub modifyage(byval a as integer) age = a end sub public sub new(byval na as string, byval a as integer) name = na age = a end sub public sub show() messagebox.show("name: " + name + " age: " + str(age)) end subend classclass teacher inherits person public sub new(byval na as string, byval a as integer) mybase.new(na, a) end sub protected sub modify(byval na as string, byval a as integer) mybase.name = na mybase.modifyage(a) end subend classclass fulltime_teacher inherits teacher public sub new(byval na as string, byval a as integer) mybase.new(na, a) end sub public sub modifyvalue(byval na as string, byval a as integer) mybase.modify(na, a) end sub public sub modifydata(byval na as string, byval a as integer) mybase.name = na mybase.modifyage(a) 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 p1 as new fulltime_teacher("david", 25) p1.show() p1.modifyvalue("kent smith", 45) p1.show() end subend class此步调输入: name: david age: 25 name: kent smith age: 45 show()是person类型的public步调,孙子类型fulltime_teacher接受之,变成fulltime_teacher类型的public步调,以是form1_click()步调能写着训令:p1.show()。name和modifyage()是person的protected分子,teacher的modify()步调里能径直运用它们。并且fulltime_teacher的modifydata()步调里能径直运用它们。然而form1_click()就没辙运用它们。modify()是teacher的protected分子,fulltime_teacher的modifyvalue()步调里能径直运用它们,但form1_click()就不行运用。以是若将上述form1_click()的训令改为如次,就不对了: protected sub form1_click( ..... ) dim p1 as new fulltime_teacher("david", 25) p1.show() p1.modify("kent smith", 45) 'error! p1.modifyage(24) 'error! p1.show() end subend classn

热门阅览

最新排行

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