第4章继承.ppt_第1页
第4章继承.ppt_第2页
第4章继承.ppt_第3页
第4章继承.ppt_第4页
第4章继承.ppt_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1、1,第四章 继承,继承是面向对象语言中重要的、功能强大的特性。下面是一个单继承实例:,基类(超类)成员在派生类(子类)的可见性?,2,关于protected关键字 就用户而言,它是private的,但对于任何继承于此类的导出类(或派生类或子类)或其它任何位于同一个包内的类来说,它却是可以访问的。(protected提供了包内访问权限) protected关键字破坏了封装特性?,3,是一个(is a) 如果继承只覆盖基类的方法(不添加在基类中没有的方法)。就意味着导出类和基类是完全相同的类型,因为它们具有完全相同的接口。结果可以用一个导出类对象来完全替代一个基类的对象。我们经常将这种情况下的基类

2、与导出类之间的关系称为is-a(是一个)关系。 在某种意义上,这是处理继承的理想方式。,4,象是一个(is like a) 有时必须在导出类型中添加新的接口元素,这样就扩展了接口。这个新的类型仍然可以替代基类,但是这种替代并不完美,因为基类无法访问新添加的方法,这种情况我们称为is-like-a(象是一个)关系。,5,有一个(has a) 代码复用是面向对象程序设计语言所提供的最了不起的优点之一。最简单地复用某个类的方式就是直接使用该类的一个对象,此外也可以将那个类的一个对象置于某个新类中,我们称为“创建一个成员对象”。因为是在使用现有的类合成新的类,所以称为组合。组合经常称为has-a(有一

3、个)关系。,6,super 复用基类(超类)行为 下面举一个复用基类(超类)方法的例子: public class Student / Attributes. private String name; private String studentId; private String majorField; private double gpa; / etc. / Accessor methods for each attribute would also be / provided; details omitted.,7,public void print() / Print the valu

4、es of all of the attributes that the Student class / knows about. (Remember: n is a newline.) System.out.println(Student Name: + this.getName() + n + Student No.: + this.getStudentId() + n + Major Field: + this.getMajorField() + n + GPA: + this.getGpa(); ,8,public class GraduateStudent extends Stude

5、nt private String undergraduateDegree; private String undergraduateInstitution; / Accessor methods for each newly added attribute would also be provided; / details omitted. / We are overriding the Student classs print method.,9,public void print() / Reuse code by calling the print method as defined

6、by the / Student superclass . super.print(); / . and then go on to do something extra - namely, print this / derived classs specific attributes. System.out.println(Undergrad. Deg.: + this.getUndergraduateDegree() + n + Undergrad. Inst.: + this.getUndergraduateInstitution(); ,10,几个问题 super关键词能否省略?为什么

7、? 能否重复使用super,例如,super.super.print()? 如果基类Student 的print()方法直接访问Student的私有属性。即未用get方法访问其私有属性。 GraduateStudent 中print()方法调用基类Student 的print()方法会不会出现问题? 如果这是一个C+应用程序,应该怎样访问基类Student 的print()方法?,11,答案 不能省略,否则会出现递归调用,不能正常终止。 不能,Java语法不允许。 不会出现什么问题。基类Student 的print()方法能够访问Student 的私有属性。 Student 的print()方

8、法是公有的, GraduateStudent 中print()方法调用Student 的print()方法,前者调用后者与后者是否访问了私有属性无关。 Student :print();,12,基类(超类)构造方法不能被继承,super可复用基类(超类)构造方法: public class Person / details omitted. public Person(String n, String s) / Initialize our attributes. / Pseudocode. do other complex things related to instantiating a

9、Person ,Person类只能识别一个构造方法签名(可以接收两个实参的构造方法),因为默认的无参构造方法被去掉了。,13,public class Student extends Person / name and ssn are inherited from Person . private String major; / Constructor with two arguments. public Student(String n, String s) / Were explicitly invoking the Person constructor that accepts two

10、/ String arguments by passing in two String arguments - namely, the / values of n and s. super(n, s); / Then, go on to do only those things that need to be done uniquely / for a Student. this.setMajor(UNDECLARED); / Pseudocode. do complex things related to instantiating a Student specifically. ,Pers

11、on类的派生类Student有两个构造方法:一个接收两个实参,另一个接受三个实参。,由于显式声明了构造方法,Student类也失去了它默认无参的构造方法。,14,/ Constructor with three arguments. public Student(String n, String s, String m) / See comments above. super(n, s); this.setMajor(m); / Pseudocode. do complex things related to instantiating a Student specifically. ,Sup

12、er()可以在子类中重用超类构造方法,必须是子类构造方法中中的第一个语句。,15,继承和构造方法 如果没有明确为类定义构造方法,Java会为这个类自动提供一个默认(无参)构造方法。相反,如果为类明确定义了构造方法,则默认(无参)构造方法不会被提供。 派生类不能继承超类构造方法。创建派生类对象时,Java总是试图执行给定类的所有祖先的构造方法。执行顺序从类层次结构的最一般类型到最特殊的类型。创建Java对象,总是Object类的构造方法首先被执行。 如果给定的超类有多个构造方法,除非用super关键字明确指定超类的某个构造方法,否则超类无参的构造方法会被自动调用。,16,继承和构造方法(续) 这

13、样会产生一个潜在的问题。例如,有两个类A和B,B是从A派生的,且A有一个有参构造方法,B没有显式构造方法,则不能通过编译!因为编译器试图为B类创建一个无参构造方法。为了达到这个目的,编译器接下来要做的是在B类的构造方法中调用A类的无参构造方法,而A类并没有这样的构造方法! 避免这个问题的最好的方法是:在编写类X时,如果需要编写构造方法,总是明确地编写一个无参的构造方法,以替换丢失的默认(无参)的构造方法。,17,多继承 有时可能需要多个基类,派生出新类。有多个基类的继承称之为多继承。例如,Professor类表示讲课的人,Student类表示听课的人。如果一个Professor想参加一门课程的

14、学习,或一个学生被邀请讲授一门课程,需要将Professor类和Student类组合在一起派生出一个新类ProfessorStudent。,18,下面讨论多继承中存在的问题。,19,public class Person private String name; / Accessor method details omitted. public String printDescription() System.out.println(getName(); / e.g., John Doe ,20,public class Student extends Person / We add two

15、attributes. private String major; private String studentId; / Accessor method details omitted. / Override this method as inherited from Person. public String printDescription( ) return getName() + + getMajor() + ; + getStudentId() + ; / e.g., Mary Smith Math; 10273 ,21,public class Professor extends

16、 Person / We add two attributes. private String title; private String employeeId; / Accessor method details omitted. / Override this method as inherited from Person. public String printDescription( ) return getName() + + getTitle() + ; + getEmployeeId() + ; / e.g., Harry Henderson Chairman; A723 ,22

17、,/ * * * Important Note: this is not permitted in Java! * * * public class StudentProfessor extends Professor and Student / Its OK to leave a class body empty; the class itself is not / REALLY empty, because it inherits the features of all / of its ancestors. ,23,小测验:下面的程序是否能够通过编译? / Person.java pub

18、lic class Person private String name; / Weve written an explicit constructor with one argument for / this (super)class; by having done so, weve LOST the / Person classs default parameterless constructor. public Person(String n) name = n; ,24,/ Note that we havent bothered to REPLACE the parameterless / constructor with one of our own design. This is going to / cause us problems, as well see in a moment. / Me

温馨提示

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

评论

0/150

提交评论