java语言程序设计基础篇原书第八版-(第十一章)精品PPT课件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
R ectan gle
-w id th : d ou b le -h eigh t: d oub le
+ R ectan gle() + R Байду номын сангаасctan gle(w id th : d ou b le, h eigh t: d ou b le) + R ectan gle(w id th : d ou b le, h eigh t: d ou b le
Chapter 11 Inheritance and Polymorphism
1
Motivations
Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer is to use inheritance.
GeometricObject1 Circle4
C ircle
-rad iu s: d ou b le
+ C ircle() + C ircle(rad iu s: d oub le) + C ircle(rad iu s: d oub le, color: S trin g ,
filled : b oolean ) + getR ad iu s(): d oub le + setR ad iu s(rad iu s: d ou b le): void + getA rea(): d ou b le + getP erim eter(): d ou b le + getD iam eter(): d ou b le + p rin tC ircle(): v oid
filled : b oolean ) + getC olor(): S trin g + setC olor(color: S trin g): void + isF illed (): b oolean + setF illed (filled : b oolean ): void + g etD ateC reated (): jav a.u til.D ate + toS trin g(): S trin g
2
Objectives
To develop a subclass from a superclass through inheritance (§11.2). To invoke the superclass’s constructors and methods using the super keyword (§11.3). To override instance methods in the subclass (§11.4). To distinguish differences between overriding and overloading (§11.5). To explore the toString() method in the Object class (§11.6). To discover polymorphism and dynamic binding (§§11.7-11.8). To describe casting and explain why explicit downcasting is necessary (§11.9). To explore the equals() method in the Object class (§11.10). To store, retrieve, and manipulate objects in an ArrayList (§11.11). To implement a Stack class using ArrayList (§11.12). To restrict access to data and methods to subclasses using the protected visibility modifier (§11.13). To prevent class extending and method overriding using the final modifier (§11.14).
C reates a G eom etricO bject. C reates a G eom etricO b ject w ith th e sp ecified color and filled
valu es. R etu rn s th e color. S ets a new color. R etu rn s th e filled p rop erty. S ets a n ew filled p rop erty. R etu rn s th e dateC reated. R etu rn s a strin g rep resen tation of th is ob ject.
T h e color o f th e ob ject (d efau lt: w h ite). In d icates w h eth er th e object is filled w ith a color (d efau lt: false). T h e d ate w h en th e object w as created.
3
Superclasses and Subclasses
G eo m etricO b ject
-color: S trin g -filled : b oolean -d ateC reated : java.util.D ate
+ G eom etricO bject() + G eom etricO b ject(color: S trin g ,