




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第10章 oop(下) 10.1 类的继承/派生 10.2 public、private、protected 10.3 me、myclass、mybase 10.4 接口(了解) n继承/派生语法 class parentclass基类或父类 end class class childclass子类或派生类 inherits parentclass end class 10.1 类的继承/派生 n继承/派生: n继承/派生:子类自动拥有父类的属性和方法 , n扩展:子类可以定义自己的属性和方法。 n覆盖(override):子类可以覆盖( override)父类的方法,即保持使用父类的 某个方法名字和参数不变,但改变方法的定义 ,即重新写过程代码。 n注意与重载(overload)的区别,覆盖经常也 被叫做重载。 10.1 类的继承/派生 n继承方式:父类parentclass可以是.net的类,也 可以是自定义的类。 n继承目的:扩展父类。以类似滚雪球的方法不断地 扩展代码。 10.1 类的继承/派生 10.1 类的继承/派生 nvb.net总祖先类: n.net框架的一切类都是从system.object类直 接或间接派生出来的。 n.net框架的一切类都直接或间接继承了 system.object类。 nsystem.object类的成员有 equals(obj)、gettype()、tostring()、 gethashcode() n所有对象都有上述的方法。 n继承例子 1、基类或父类 public class person public name as string protected id as string public overridable sub show_id() msgbox(id) end sub end class 10.1 类的继承/派生 2、子类或派生类 public class student inherits person继承 public school_id as string public overrides sub show_id() msgbox(id & ”-” & school_id)覆盖 end sub end class 10.1 类的继承/派生 3、定义基类对象、子类对象 dim x as new person dim y as new student 4、使用基类对象、子类对象 x.show_id()显示id y.show_id() 显示id-school_id 10.1 类的继承/派生 1、public属性:不能用对象引用(第九章) public class person public name as string end class dim x as new person msgbox()正确 10.2 public、private、protected 1、public属性:能在子类中引用 public class person public name as string end class public class student inherits person继承 public sub show_name() msgbox(name)正确 end sub end class 10.2 public、private、protected 2、private属性:不能用对象引用 public class person private name as string end class dim x as new person msgbox()错误 10.2 public、private、protected 2、private属性:不能在子类中引用 public class person private name as string end class public class student inherits person继承 public sub show_name() msgbox(name)错误 end sub end class 10.2 public、private、protected 3、protected属性:不能用对象引用。 public class person protected name as string end class dim x as new person msgbox(x. name)错误 10.2 public、private、protected 3、protected属性:能在子类中引用。 public class person protected name as string end class public class student inherits person继承 public sub show_name() msgbox(name)正确 end sub end class 10.2 public、private、protected nme:在类定义中引用自己的对象。 public class person public name as string public sub set_name(byval name as string) m=name注意两个name的区别。 end sub end class 10.3 me、myclass、mybase nmybase:在类定义中引用自己的基类。 1、定义基类 public class parent public overridable sub foo() msgbox(”parent”) end sub end class 10.3 me、myclass、mybase 2、定义子类 public class child inherits parent public overrides sub foo()覆盖 msgbox(”child”) end sub public sub f() mybase.foo()使用基类的foo end sub end class 10.3 me、myclass、mybase nmyclass:在类定义中引用自己的代码。 1、定义基类 public class parent public sub foo() show() end sub public overridable sub show() msgbox(”parent”) end sub end class 10.3 me、myclass、mybase 2、定义子类 public class child inherits parent public overrides sub show() 覆盖 msgbox(”child”) end sub end class 10.3 me、myclass、mybase 3、由于对象不同,foo指向的代码不同 dim x as new parent x.foo()显示parent dim y as new child y.foo()显示child 4、在基类中修改: public sub foo() myclass.show() end sub 10.3 me、myclass、mybase 5、对象不同,foo指向的代码相同 dim x as new parent x.foo()显示parent dim y as new child y.foo()显示parent 10.3 me、myclass、mybase q接口:与类相似,包含属性、成员函数定义,但只有 成员函数的名称和参数定义,无代码实现。由于接口 成员函数无代码实现,因此不能从接口直接定义对象 。 q接口语法: public interface ifa sub init(byval x as string) end interface q接口实
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年网络舆情监测与危机公关应对策略试题及答案
- 2025年网络营销与品牌建设考试试题及答案
- 2025年表面改性金属材料项目建议书
- 2025年雷达、无线电导航及无线电遥控设备项目建议书
- 沛县期末3年级数学试卷
- 梅州高二联考数学试卷
- 六年级下册徐州数学试卷
- 培优三年级下数学试卷
- 六数上册数学试卷
- 宁波海曙和江北数学试卷
- 【化学 山东版】2025年普通高等学校招生选择性考试(原卷版)
- JGJ162-2025《建筑施工模板安全技术规范》
- 美业合伙人合同范本
- 海砂开采施工合同协议
- 职业健康检查质量管理工作手册
- 资产并购咨询合同协议
- 商业入驻意向合同协议
- 2025统编版(2024)小学道德与法治一年级下册《第13课-快乐儿童节》教学设计
- 2025-2030中国精液分析行业市场发展趋势与前景展望战略分析研究报告
- 教师招考试题及答案临沂
- 2025年咖啡师职业技能竞赛参考试题库500题(含答案)
评论
0/150
提交评论