Java精解案例教程 第6课 Java类设计.ppt_第1页
Java精解案例教程 第6课 Java类设计.ppt_第2页
Java精解案例教程 第6课 Java类设计.ppt_第3页
Java精解案例教程 第6课 Java类设计.ppt_第4页
Java精解案例教程 第6课 Java类设计.ppt_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六章 Java类设计 本章内容 Java类的继承方法的重载 重写多态性及其应用成员访问控制对象的创建和初始化细节几种常用的Java类 类的继承 1 为描述和处理个人信息 定义类Person publicclassPerson publicStringname publicintage publicDatebirthDate publicStringgetInfo 类的继承 2 为描述和处理学生信息 定义类Student publicclassStudent publicStringname publicintage publicDatebirthDate publicStringschool publicStringgetInfo 类的继承 3 通过继承 简化Student类的定义 publicclassPerson publicStringname publicintage publicDatebirthDate publicStringgetInfo publicclassStudentextendsPerson publicStringschool publicStringgetInfor publicvoidshow publicvoidshow intt 继承对象的生成 Studentst newStudent 0600 st st ptr PublicStringgetInfor name age BirthDate st ptr PublicStringgetInfor name age BirthDate School st ptr PublicStringgetInfor name age BirthDate PublicStringgetInfor School st ptr PublicStringgetInfor name age BirthDate PublicStringgetInfor Publicvoidshow Publicvoidshow inta School st ptr PublicStringgetInfor name age BirthDate PublicStringgetInfor Publicvoidshow Publicvoidshow inta School 类的继承 4 类继承语法规则 class extends Object类是所有Java类的最高层父类Java只支持单继承 不允许多重继承一个子类只能有一个父类一个父类可以继承出多个子类 单继承举例 Java类的访问权限 访问控制 可以对Java类中定义的属性和方法进行访问控制 规定不同的保护等级 Publicclass defaultclass public class class publicclass defaultclass public class defaultclass publicclass public default class Publicclass public 访问控制举例 1 classParent privateintf1 1 intf2 2 protectedintf3 3 publicintf4 4 privatevoidfm1 System out println infm1 f1 f1 voidfm2 System out println infm2 f2 f2 protectedvoidfm3 System out println infm3 f3 f3 publicvoidfm4 System out println infm4 f4 f4 访问控制举例 2 0601 classChildextendsParent privateintc1 21 publicintc2 22 privatevoidcm1 System out println incm1 c1 c1 publicvoidcm2 System out println incm2 c2 c2 publicstaticvoidmain Stringargs inti Parentp newParent i p f2 i p f3 i p f4 p fm2 p fm3 p fm4 Childc newChild i c f2 i c f3 i c f4 i c c1 i c c2 c cm1 c cm2 访问控制分析 f1 private f2 default c2 public f3 protected f4 public c1 private 内存空间中子类对象携带的数据 子类的对象可以调用的方法 fm2 default fm3 protected fm4 public cm2 public cm1 private 父类Parent和子类Child在同一包中定义时 方法的重写 0603 在子类中可以根据需要对从父类中继承来的方法进行改造 方法的重写重写方法必须和被重写方法具有相同的方法名称 参数列表和返回值类型重写方法不能使用比被重写方法更严格的访问权限 0603 方法重写举例 1 publicclassPerson protectedStringname protectedintage protectedDatebirthDate java util包中定义过类DatepublicStringgetInfo return Name name n age age publicclassStudentextendsPerson protectedStringschool publicStringgetInfo return Name name nage age nschool school 方法重写举例 2 publicclassParent publicvoidmethod1 publicclassChildextendsParent privatevoidmethod1 illegal publicclassUseBoth publicvoiddoOtherThing Parentp1 newParent Childp2 newChild p1 method1 p2 method1 关键字super 在Java类中使用super来引用父类的成分super可用于访问父类中定义的属性super可用于调用父类中定义的成员方法super可用于在子类构造方法中调用父类的构造方法super的追溯不仅于直接父类 关键字super举例 publicclassPerson privateStringname privateintage privateDatebirthDate publicStringgetInfo return Name name nage age publicclassStudentextendsPerson privateStringschool NewOriental publicStringgetSchool returnschool publicStringgetInfo 调用父类的方法returnsuper getInfo nschool school st ptr PublicStringgetInfor name age BirthDate PublicStringgetInfor Publicvoidshow Publicvoidshow inta School ps 多态性 1 多态 在Java中 父类对象的指针可以指向它的所有继承类 一个对象只能有一种确定的数据类型一个引用类型变量可能指向 引用 多种不同类型的对象Personp newStudent Objecto newPerson o newStudent 多态性 2 一个引用类型变量如果声明为父类的类型 但实际引用的是子类对象 那么该变量就不能再访问子类中添加的属性和方法Studentm newStudent m school pku 合法Persone newStudent 虚方法调用 VirtualMethodInvocation 正常的方法调用Persone newPerson e getInfo Studente newStudent e getInfo 虚拟方法调用 多态情况下 Persone newStudent e getInfo 编译时类型和运行时类型编译时多态 重载来实现运行时多态 虚方法来调用 Dt java instanceof操作符 0607 publicclassPersonextendsObject publicclassStudentextendsPerson publicclassGraduateextendsPerson publicvoidmethod1 Persone if einstanceofStudent 处理Student类型及其子类类型对象 elseif einstanceofGraduate 处理Graduate类型及其子类类型对象 else 处理Person类型对象 对象造型 Casting 对Java对象的强制类型转换称为造型在造型前可以使用instanceof操作符测试一个对象的类型从子类到父类的类型转换可以自动进行从父类到子类的类型转换必须通过造型 强制类型转换 实现无继承关系的引用类型间的转换是非法的 对象造型举例 publicclassTest Person及Student类的定义见第17页publicvoidmethod Persone System out pritnln e getschool 非法if eintstanceofStudent Studentme Student e System out pritnln me getschool publicstaticvoidmain Stirngargs Testt newTest Studentm newStudent t method m 方法名重载 在同一个类中可以定义多个同名方法 方法名重载publicclassPrintStream publicvoidprintln inti publicvoidprintln floatf publicvoidprintln Strings 重载方法的参数列表必须不同重载方法的返回值类型可以相同 也可以不同 构造方法重载 0608 构造方法重载举例 publicclassPerson publicPerson Stringname intage Dated publicPerson Stringname intage publicPerson Stringname Dated 构造方法重载 参数列表必须不同可以在构造方法的第一行使用this关键字调用其它 重载的 构造方法 构造方法重载举例 publicclassPerson privateStringname privateintage privateDatebirthDate publicPerson Stringname intage Dated this name name this age age this birthDate d publicPerson Stringname intage this name age null publicPerson Stringname Dated this name 30 d publicPerson Stringname this name 30 构造方法不能继承 子类继承父类所有的成员变量和成员方法 但不继承父类的构造方法一旦显式定义了构造方法 则系统不再提供默认构造方法 调用父类构造方法 0609 在子类的构造方法中可使用super argument list 语句调用父类的构造方法如果子类的构造方法中没有显示地调用父类构造方法 也没有使用this关键字调用重载的其它构造方法 则系统默认调用父类无参数的构造方法如果子类构造方法中既未显式调用父类构造方法 而父类中又没有无参的构造方法 则编译出错 调用父类构造方法举例 1 1publicclassPerson 23privateStringname 4privateintage 5privateDatebirthDate 67publicPerson Stringname intage Dated 8this name name 9this age age 10this birthDate d 11 12publicPerson Stringname intage 13this name age null 14 15publicPerson Stringname Dated 16this name 30 d 17 18publicPerson Stringname 19this name 30 20 21 22 调用父类构造方法举例 2 1publicclassStudentextendsPerson 2privateStringschool 34publicStudent Stringname intage Strings 5super name age 6school s 7 8publicStudent Stringname Strings 9super name 10school s 11 12publicStudent Strings 编译出错 nosuper 13school s 14 15 对象构造和初始化细节 分配存储空间并进行默认的初始化按下述步骤初始化实例变量绑定构造方法参数如有this 调用 则调用相应的重载构造方法 然后跳转到步骤5显式或隐式追溯调用父类的构造方法 Object类除外 进行实例变量的显式初始化操作执行当前构造方法的方法体 构造和初始化对象举例 publicclassObject publicObject publicclassPersonextendsObject privateStringname privateintage 0 privateDatebirthDate publicPerson Stringn Dated implicitsuper name n birthDate d publicPerson Stringn this n null publicclassStudentextendsPerson privateStringschool publicStudent Stringn Stringd super n school d Ex2初始化过程引申 publicclassPerson privateStringname privateintage 0 privateDatebirthDate privateStringinfo publicPerson Stringn Dated step3name n birthDate d info getInfo publicPerson Stringn this n null step2publicStringgetInfo return Name name nage age nBirthDate birthDate publicclassStudentextendsPerson privateStringschool publicStudent Stringn Stringd step0super n step1school d publicStringgetInfo step4returnsuper getInfo nSchool school Object类 Object类是所有Java类的根父类如果在类的声明中未使用extends关键字指明其父类 则默认父类为Object类publicclassPerson 等价于 publicclassPersonextendsObject 操作符与equals方法 操作符与equals方法的区别 引用类型比较引用 基本类型比较值 equals 方法只能比较引用类型 可以比较引用类型及基本类型 特例 当用equals 方法进行比较时 对类File String Date及封装类 WrapperClass 来说 是比较类型及内容而不考虑引用的是否是同一个实例 用 进行比较时 符号两边的数据类型必须一

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论