版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1,第11章 继承和多态,2,引言,假设我们将定义类模型的圆,矩形,三角形。这些类都有许多共同的特点。设计这类以避免冗余的最好方法是什么?答案是使用继承。,3,学习目标,通过继承由父类创建子类(11.2节)。 使用关键字super调用父类的构造方法和方法(11.3节)。 在子类中覆盖方法(11.4节)。 区分覆盖和重载的不同(11.5节)。 探究object类中的tostring()方法(11.6节)。 理解多态性和动态绑定(11.7-11.8节)。 描述转换并解释显示向下转换的必要性(11.9节)。 探究object类中的equeals()方法(11.10节)。 存储,提取和操作ArrayL
2、ist中的对象(11.11节)。 使用ArrayList实现Stack类(11.12节)。 使用可见性修饰符protected使父类中的数据和方法可以被子类访问(11.13节)。 使用修饰符final防止类的拓展以及方法的覆盖(11.14节)。,4,父类和子类,GeometricObject1,Circle4,Rectangle1,TestCircleRectangle,Run,5,父类的构造方法被继承吗,不,构造方法不能被继承。他们是显式或隐式调用。显式调用使用super关键词。 一个构造方法可用来构建一个类的实例。不像属性和方法,父类的构造方法是不被子类继承的。它们只能在子类的构造方法中用
3、关键字super调用。,6,构造方法链,构造方法可以调用重载的构造方法或它的父类的构造方法。如果它们都没有被显式的调用,编译器就回自动地将super()作为构造方法的第一条语句,例如:,7,使用关键字super,调用父类的构造方法。 调用父类的方法。,关键字super是指使用这个super时的类的父类。关键字super可以用于两种途径:,8,我们必须使用关键字super调用父类的构造函数 在子类中调用父类的构造函数的名字,将导致一个语法错误 Java要求关键词super必须出现在子类构造方法的第一个行。,使用关键字super,9,构造方法链,public class Faculty exten
4、ds Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invo
5、ked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,构建一个类的实例时,将会调用沿着继承链的所有父类的构造方法。这就是构造方法链。,10,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty(
6、) System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Pers
7、on() System.out.println(1) Persons no-arg constructor is invoked); ,1. 从main方法开始,animation,11,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person p
8、ublic Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,2. 调用构造方法 Faculty,animation,
9、12,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Em
10、ployees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,3. Invoke Employees no-arg constructor,animation,13,跟踪执行,public class Faculty extends Employee public static void main(
11、String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) Syst
12、em.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,4. Invoke Employee(String) constructor,animation,14,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Fac
13、ultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1)
14、Persons no-arg constructor is invoked); ,5. Invoke Person() constructor,animation,15,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Emp
15、loyee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,6. Execute println,animation,16,跟踪执行,
16、public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees
17、no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,7. Execute println,animation,17,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty();
18、public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Employee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Per
19、son public Person() System.out.println(1) Persons no-arg constructor is invoked); ,8. Execute println,animation,18,跟踪执行,public class Faculty extends Employee public static void main(String args) new Faculty(); public Faculty() System.out.println(4) Facultys no-arg constructor is invoked); class Empl
20、oyee extends Person public Employee() this(2) Invoke Employees overloaded constructor); System.out.println(3) Employees no-arg constructor is invoked); public Employee(String s) System.out.println(s); class Person public Person() System.out.println(1) Persons no-arg constructor is invoked); ,9. Exec
21、ute println,animation,19,例如一个父类没有构造方法,public class Apple extends Fruit class Fruit public Fruit(String name) System.out.println(Fruits constructor is invoked); ,在下面的程序中找出错误:,20,声明一个父类,一个类从父类的属性和方法。我们也可以:添加新的属性添加新的方法重写父类的方法,21,重写父类的方法,可以重写Circle类的printCircle()方法:,public void printCircle() System.out.
22、println(The circle is created + super.getDateCreated() + and the radius is + radius); ,22,重写父类中的方法,子类从父类继承方法。有时,子类需要修改父类中定义的方法的实现,这称做方法的覆盖,public class Circle extends GeometricObject / Other methods are omitted /* Override the toString method defined in GeometricObject */ public String toString() re
23、turn super.toString() + nradius is + radius; ,23,注意,仅当实例方法可以访问时,它才能被覆盖。 因为私有方法在它的类本身以外是不能访问的,所以它不能被覆盖。 如果子类中定义的方法在父类中是私有的,那么这两个方法没有关系。,24,注意,与实例方法一样,静态方法可以继承。 然而,静态方法不能被覆盖。 如果父类中定义的静态方法在子类中重新定义,那么定义在父类中的静态方法将被隐藏。,25,覆盖和重载,java中,覆盖是指在子类中定义与父类同名且参数类型和个数都相同的方法。故覆盖要求方法头与父类方法完全一致。,26,对象类object和它的方法,在Java
24、的每一类是继承自java.lang.Object类。 如果在定义一个类时没有指定继承性,那么这个类的父类被默认是Object。 例如:,27,Object的toString()方法,调用一个对象的toString()会返回一个描述该对象的字符串。 默认情况下,它返回一个由对象所属的类名,at符号()以及对象十六进制形式的内存地址组成的字符串。,Loan loan = new Loan(); System.out.println(loan.toString();,这些代码会显示Loan15037e5的字符串。这个信息不是很有用,或者说没有声什么信息量。 通常应该覆盖这个toString()方法,
25、这样,它可以返回一个代表该对象的描述性的字符串。,28,多态性,动态绑定和泛型编程,public class PolymorphismDemo public static void main(String args) m(new GraduateStudent(); m(new Student(); m(new Person(); m(new Object(); public static void m(Object x) System.out.println(x.toString(); class GraduateStudent extends Student class Student e
26、xtends Person public String toString() return Student; class Person extends Object public String toString() return Person; ,方法m采用object类型的参数。可以用任意对象作为参数来调用m方法。,使用父类对象的地方都可以使用子类的对象,这就是通常所说的多态。,当执行方法m(object x)时,调用参数x的toString方法。X可能会是GradatueStudent,Student,Person或者Object的实例。类GradatueStudent,Student,P
27、erson或者Object都有它们对自己toString方法的实现。使用哪个实现取决于运行x时的实际类型。,PolymorphismDemo,Run,29,动态绑定,动态绑定的工作原理如下: 假设一个对象实例的类C1,C2,Cn-1,Cn,C1是C2子类,C2是C3的子类,Cn-1是Cn的子类。也就是说,Cn是最通用的类,C1是最特殊的类。 在Java中,Cn是Object类。如果对象o调用一个方法P,java虚拟机会依次在类C1,C2,Cn-1,Cn中查找方法p的实现,在这个秩序,直到它被发现。 一旦找到,停止查找然后调用这个第一个找到的实现。,30,方法匹配与绑定,匹配的方法签名和绑定方法
28、的实现是两个问题。 编译器在编译的时候根据参数类型、个数、顺序找到一个匹配的方法。 一种方法可以在几个不同的子类中实现。 Java虚拟机在运行时动态绑定方法实现。,31,转换对象,我们已经使用过转换运算符把一种基本类型变量转换为另一种基本类型。 转换也可以用来把一种类类型的对象转换为继承体系结构中的另一种类类型的对象。 在上一节中,语句: m(new Student(); 将对象new Student()复制给一个object类型的参数。这条语句等价于: Object o = new Student(); / 隐式转换 m(o);,所以, Object o = new Student()是合法
29、的,它称为隐式转换,32,为什么需要转换?,使用下面的语句把对象引用o赋值给Student类型的变量: Student b = o; 将会发生编译错误。 为什么语句 Object o = new Student() 可以运行,而语句 Student b = o; 不行呢?原因是Student对象总是Object的实例,但是,Object对象不一定是Student实例。即使可以看到o实际上是一个Student的对象,但是编译器还没有聪明到懂得这一点。为了告诉编译器o就是Student对象,就要使用显式转换。它的语法与基本类型的语法很相似,用圆括号把目标类型的参数括住,然后放到要转换的对象前面,如
30、下所示: Student b = (Student)o; / 显式转换,33,从父类到子类的转换,当把一个父类的实例转换为它的子类变量时,必须使用转换标记号“(子类名)”进行显式转换,但有时不一定转换成功。 Apple x = (Apple)fruit; Orange x = (Orange)fruit;,34,instanceof 运算符,使用运算符instanceof确保对象是另一个对象的实例: Object myObject = new Circle(); . / Some lines of code /* Perform casting if myObject is an instan
31、ce of Circle */ if (myObject instanceof Circle) System.out.println(The circle diameter is +(Circle)myObject).getDiameter(); . ,35,小窍门,为了更好的理解类型转换,可以认为它们类似于水果,苹果,橘子之间的关系。 其中水果类Fruit是苹果Apple和橘子类Orange的父类,苹果是水果,所以,总可以将Apple的实例安全的赋值给Fruit变量。 但是,水果不一定是苹果,所以必须进行显式转换才能将Fruit的实例赋值给Apple的变量。,36,示例:演示多态性与转换,程序创建两个几何对象: 一个圆和一个矩形,调用displayGeometricObject方法显示它们。 如果对象是一圆displayGeometricObject显示面积和周长。 如果对象是一个矩形,这个方法显示它的面积。,TestPolymorphismC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 部队年度考核奖惩制度
- 员工食堂管理奖惩制度
- 团建团队奖惩制度范本
- 学校组织部门奖惩制度
- 违规野外用火奖惩制度
- 华为奖惩制度实施细则
- 厂务处员工奖惩制度范本
- 保安员疫情防控奖惩制度
- 仪班组考核奖惩制度
- 定制工厂奖惩制度范本
- 第七人民医院供应商来访接待须知
- (高清版)TDT 1056-2019 县级国土资源调查生产成本定额
- 《跟单信用证统一惯例》UCP600中英文对照版
- 材料设备验收移交单
- 输煤栈桥彩钢板更换施工方案
- PCI术后常见并发症及处理
- GB/T 35163-2017载重汽车轮胎湿路面相对抓着性能试验方法
- 【公开课】排列、排列数+课件高二下学期数学人教A版(2019)选择性必修第三册
- 溢油应急处置培训讲义
- 袁晓萍:认识圆柱
- 胜任特征辞典
评论
0/150
提交评论