第6章类的继承性 理解类的继承性概念以及父类和子类的关糸 习 学习使用修饰符 protect、fnal、 abstract 理解多态性 具。使用甚口和内部类 标熟悉程序开发的过程和类的设计原则
第6章类的继承性 )理解类的继承性概念以及父类和子类的关系 )学习使用修饰符protect、final、abstract )理解多态性 )使用接口和内部类 )熟悉程序开发的过程和类的设计原则
61父类和子类 uperclass Circle Circle methods Circle data Inheritance Subclass Cylinder Circle methods Circle data Cylinder Methods Cylinder Data Superclass Subclass UML Diagram Circle CⅤ linder radius length getRadius setRadius findArea setlength find Volume
6.1 父类和子类 Superclass Circle Circle Methods Circle Data Inheritance Cylinder Circle Methods Cylinder Methods Circle Data Cylinder Data Subclass Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume Superclass Subclass UML Diagram
Creating a Subclass Creating a subclass extends data and methods from the superclass. You can also e Add new data Add new methods Override the methods of the superclass Cylinder class
Creating a Subclass Creating a subclass extends data and methods from the superclass. You can also: ) Add new data ) Add new methods ) Override the methods of the superclass Cylinder Class
Example 6.1 Testing Inheritance Objective: Create a Cylinder object and explore the relationship between the ylinder and circle classes TestCylinder R un
Example 6.1 Testing Inheritance ) Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes. TestCylinder Run
6.2 The Object class The object class is the root of all Java classes c The tostring()method returns a string representation of the object The equals()method compares the contents of two objects The clone() method copy objects
6.2 The Object Class ) The Object class is the root of all Java classes. ) The toString() method returns a string representation of the object. ) The equals() method compares the contents of two objects. ) The clone() method copy objects
6.3修饰符 The protected Modifier g protected修饰符应用于类的数据和方法 上。公共类中保护的数据或方法可以被同 包中的任何类或它的子类访问,即使子 类在不同的包中也可以
6.3 修饰符 The protected Modifier ) protected 修饰符应用于类的数据和方法 上。公共类中保护的数据或方法可以被同 一包中的任何类或它的子类访问,即使子 类在不同的包中也可以
The protected Modifier pacakge pI acakge p2 C2是c1 class c class c2 extends C1 的子类 protected intx x can be read or modified in C2 C1和c3 在同 class C3 class C4 个包中 Cl cl CI c.x can be read or clx cannot be rea C4与c1 modified nor modified 不在同 个包 中
The protected Modifier pacakge p1 class C1 protected int x class C3 C1 c1; c1.x can be read or modified pacakge p2 class C2 extends C1 x can be read or modified in C2 class C4 C1 c1; c1.x cannot be read nor modified C1和C3 在同一 个包中 C2是C1 的子类 C4与C1 不在同 一个包 中
The final modifier The final class cannot be extended: final class Math The final variable is a constant. final static double pi 3.14159 The final method cannot be modified by its subclasses
The final Modifier ) The final class cannot be extended: final class Math {...} ) The final variable is a constant: final static double PI = 3.14159; ) The final method cannot be modified by its subclasses
The abstract modifier The abstract class Cannot be instantiated Should be extended and implemented in subclasses The abstract method Method signature without implementation
The abstract Modifier ) The abstract class – Cannot be instantiated – Should be extended and implemented in subclasses ) The abstract method – Method signature without implementation
64多态性和动态绑定 G多态性( polymorphism)是一个希腊 单词,意为多种形式。 a在Java中,可以在同一类或不同类中 定义名称相同但操作不同的多个方法, 多态性指的是运行时判断应该执行哪个 方法代码的能力。这种能力也叫做动态 绑定
6.4 多态性和动态绑定 )多态性(polymorphism)是一个希腊 单词,意为多种形式。 )在Java中,可以在同一类或不同类中 定义名称相同但操作不同的多个方法, 多态性指的是运行时判断应该执行哪个 方法代码的能力。这种能力也叫做动态 绑定