




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行政管理学与技术结合试题及答案
- 工程管理考试复习试题及答案
- 工程经济中的财务分析试题及答案
- 通讯火灾应急预案(3篇)
- 城镇老旧供水管网更新改造工程项目规划设计方案
- 行政管理学政策建议方案试题及答案
- 2025年智慧物流园区资金申请项目可持续发展与生态环境保护报告
- xx市加氢站项目可行性研究报告
- 2025年5G网络技术在医疗行业应用可行性研究报告
- 2025年汽车内饰材料环保生产技术革新报告
- 2024年成都市城市管理委员会所属事业单位招聘真题
- 警犬培训授课课件
- 2025年四川绵阳交通发展集团有限责任公司招聘笔试参考题库附带答案详解
- 成本控制在质量管理中的策略试题及答案
- 人工智能在药物研发中的辅助作用与潜力
- 作风建设学习教育查摆问题清单及整改措施
- 2025届河北省石家庄第一中学高三下学期二模地理试题及答案
- 2024年山东开放大学招聘考试真题
- PSP问题解决流程分析
- 生活生命安全试题及答案
- 语文-华大新高考联盟2025届高三3月教学质量测评试题+答案
评论
0/150
提交评论