收藏 分享(赏)

第4章继承.ppt

上传人:gnk289057 文档编号:9992600 上传时间:2019-09-25 格式:PPT 页数:24 大小:100.50KB
下载 相关 举报
第4章继承.ppt_第1页
第1页 / 共24页
第4章继承.ppt_第2页
第2页 / 共24页
第4章继承.ppt_第3页
第3页 / 共24页
第4章继承.ppt_第4页
第4页 / 共24页
第4章继承.ppt_第5页
第5页 / 共24页
点击查看更多>>
资源描述

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 values of all

4、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

5、Student 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 的pri

8、nt()方法是公有的, 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

9、a 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.,Person类的派生类S

11、tudent有两个构造方法:一个接收两个实参,另一个接受三个实参。,由于显式声明了构造方法,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. ,Super()可以在子类中重用超

12、类构造方法,必须是子类构造方法中中的第一个语句。,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 attributes.private

15、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 Person / We add

16、 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,/ * * * Impo

17、rtant 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 public class Pe

18、rson 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 w

19、ith one of our own design. This is going to/ cause us problems, as well see in a moment./ Methods . details omitted. / Student.java public class Student extends Person / Attributes . details omitted./ NO EXPLICIT CONSTRUCTORS PROVIDED!/ Were going to be “lazy,“ and let Java provide us with/ a default parameterless constructor for the Student class./ Methods . details omitted. ,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报