《继承和多态》PPT课件.ppt
《《继承和多态》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《继承和多态》PPT课件.ppt(40页珍藏版)》请在三一办公上搜索。
1、第09章 继承和多态,章节内容,9.1 引言9.2父类和子类9.3 使用super关键字9.4 方法覆盖9.5 覆盖和重载9.6 对象类object和它的tostring方法9.7 多态性、动态绑定和一般程序设计9.8 对象转换和instanceof运算符9.9 数组线性表ArrayList类9.10自定义栈类9.11 protected数据和方法9.12 final类、方法和变量,9.1 引言,在面向对象中,从已有的类派生出新类,叫做继承。超类=父类=基类次类=子类=扩展类=派生类Java中使用关键字extends来实现类的继承。例如:class Circle extends Geometr
2、icObject 类体;创建了一个Circle类,它继承自GeometricObject类。子类继承了父类所有的成员,还可以包含自己的数据成员和方法成员,9.2父类和子类,9.2父类和子类父类GeometricObject,public class GeometricObject private String color=white;private boolean filled;private dateCreated;public GeometricObject()dateCreated=new();public String getColor()return color;public voi
3、d setColor(String color)this.color=color;public boolean isFilled()return filled;,public void setFilled(boolean filled)this.filled=filled;public getDateCreated()return dateCreated;public String toString()return created on+dateCreated+ncolor:+color+and filled:+filled;,9.2父类和子类子类Circle,public class Cir
4、cle extends GeometricObject private double radius;public Circle()public Circle(double radius)this.radius=radius;public double getRadius()return radius;public void setRadius(double radius)this.radius=radius;,public double getArea()return radius*radius*Math.PI;public double getDiameter()return 2*radiu
5、s;public double getPerimeter()return 2*radius*Math.PI;public void printCircle()System.out.println(The circle is created+getDateCreated()+and the radius is+radius);,9.2父类和子类 子类Rectangle,public class Rectangle extends GeometricObject private double width;private double height;public Rectangle()public
6、Rectangle(double width,double height)this.width=width;this.height=height;public double getWidth()return width;public void setWidth(double width)this.width=width;,public double getHeight()return height;public void setHeight(double height)this.height=height;public double getArea()return width*height;p
7、ublic double getPerimeter()return 2*(width+height);,9.2 父类和子类TestCircle测试类,/TestCircle.java:Demonstrate private modifierpublic class TestCircle/*Main method*/public static void main(String args)/Create a Circle with radius 5.0 Circle myCircle=new Circle(5.0);System.out.println(The area of the circle
8、 of radius+myCircle.getRadius()+is+myCircle.getArea();/Increase myCircles radius by 10%myCircle.setRadius(myCircle.getRadius()*1.1);System.out.println(The area of the circle of radius+myCircle.getRadius()+is+myCircle.getArea();,9.3 使用super关键字,super关键字的功能调用父类的构造方法:super();super(参数);必须出现在子类构造方法的第一行调用父
9、类的方法:super.方法名(参数);return 2*super.findArea()+(2*getRadius()*Math.PI)*length;,9.3 使用super关键字,9.3.2 构造方法链创建派生类对象时,要首先创建其中包含的基类子对象,也就是说在派生类的构造方法中,要首先调用基类的构造方法。通常,派生类构造方法中的第一条语句都是使用关键字super调用基类的构造方法;如果没有使用关键字super显式地调用基类的构造方法,则编译器将向派生类构造方法中添加一条语句super(),调用基类默认的构造方法,它是派生类构造方法的第一条语句。在任何情况下,构造一个类的实例时,将会沿着继
10、承链调用所有基类的构造方法,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)Emplo
11、yees 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);,Trace Execution,public class Faculty extends Employee public static void main(String args)new Faculty();public Faculty()System.o
12、ut.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 Person()System.out.prin
13、tln(1)Persons no-arg constructor is invoked);,1.Start from the main method,animation,Trace Execution,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 publ
14、ic 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.Invoke Faculty constructor,animation,Tr
15、ace Execution,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)Emplo
16、yees 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,Trace Execution,public class Faculty extends Employee public static void main(St
17、ring 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)System.out.printl
18、n(s);class Person public Person()System.out.println(1)Persons no-arg constructor is invoked);,4.Invoke Employee(String)constructor,animation,Trace Execution,public class Faculty extends Employee public static void main(String args)new Faculty();public Faculty()System.out.println(4)Facultys no-arg co
19、nstructor 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)Persons no-arg constructo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 继承和多态 继承 PPT 课件

链接地址:https://www.31ppt.com/p-5568491.html