![chapter 10 Abstract Classes and Interfaces_第1頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d21.gif)
![chapter 10 Abstract Classes and Interfaces_第2頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d22.gif)
![chapter 10 Abstract Classes and Interfaces_第3頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d23.gif)
![chapter 10 Abstract Classes and Interfaces_第4頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d24.gif)
![chapter 10 Abstract Classes and Interfaces_第5頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d25.gif)
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter10 Abstract Classes and InterfacesContentsContentsPart I Abstract Classes1Part II Interface2Part III The Difference Between Them3CowLionTigerAnimalCarnivorousHerbivorousSheepAnimal a = new Animal()Sheep b = new Sheep()Animals c = new Sheep()abstrct classcompile errorCircleRectangleArcLineGeom
2、etryGeometry a = new Geometry()Circle b = new Circle()Geometry c = new Circle() abstrct class10.1 Abstract Class10.1 Abstract ClassModifier abstract class className Modifier abstract returnType methodName(paramList);Modifier abstract returnType methodName(paramList)This Style is ErrorNote 1 : If we
3、use the keyword abstract to modify the method, this method is abstract method. if we use it to modify the class, it is abstract class. Note 2: If the class have the abstract method, it must be defined as the abstract class. The abstract class can have no abstract method.Note 3: The abstract method h
4、ave no any implementation. Geometry-dateCreated:java.util.Date Rectangle +draw():void +showCreateTime():void +draw():void Circle +draw():void import java.util.Date;public abstract class Geometry public Date dateCreated; public Geometry()dateCreated = new Date(); public void showCreatTime() System.ou
5、t.println(created time is : + dateCreated); public abstract void draw();public class Circle extends Geometry public void draw() System.out.println(This is a circle); public class Rectangle extends Geometry public void draw() System.out.println(This is a Rectangle); public class Test public static vo
6、id main(String args) Circle c = new Circle(); c.showCreatTime(); c.draw(); Note 4: We cant create an object from the abstract class Note 5: The abstract class must be inherited, abstract method must be override in the subclass. This point is opposite with the final class or final method. public clas
7、s Test public static void main(String args) Geometry c = new Geometry(); Cirle c = new Circle();Geometry c = new Circle();public abstract class B public abstract void Add(int a, int b);public class C public abstract void Add(int a, int b);public abstract class A public class D extends B public void
8、Multi(int a, int b)public class E extends B public void Add(int a, int b) B b1 = new B()B b2 = new E()Example II抽象類(lèi)記憶竅門(mén):抽象類(lèi)記憶竅門(mén):(1)如果一個(gè)方法用)如果一個(gè)方法用abstract修飾,它就是抽象方法;一個(gè)類(lèi)用修飾,它就是抽象方法;一個(gè)類(lèi)用abstract修飾,它就是抽象類(lèi);修飾,它就是抽象類(lèi);(2)如果一個(gè)類(lèi)中包含抽象方法,則該類(lèi)是抽象類(lèi);抽象類(lèi)里可以沒(méi))如果一個(gè)類(lèi)中包含抽象方法,則該類(lèi)是抽象類(lèi);抽象類(lèi)里可以沒(méi)有抽象方法。有抽象方法。(3)抽象方法沒(méi)有任何代碼實(shí)現(xiàn))
9、抽象方法沒(méi)有任何代碼實(shí)現(xiàn)(4)抽象類(lèi)必須被繼承,抽象方法必須被子類(lèi)重寫(xiě);這一點(diǎn)和)抽象類(lèi)必須被繼承,抽象方法必須被子類(lèi)重寫(xiě);這一點(diǎn)和final類(lèi)和類(lèi)和final方法不同,方法不同,final類(lèi)不能被繼承,類(lèi)不能被繼承,final方法不能被重寫(xiě)方法不能被重寫(xiě)(5)不能從抽象類(lèi)生成實(shí)例。)不能從抽象類(lèi)生成實(shí)例。 AnimalnameenjoyliveOnEarth Catenjoy Dogenjoy參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程public abstract class Animal public String name; public abstract void enjoy(); publ
10、ic void liveOnEarth() System.out.println(name + live on the Earthe); public class Cat extends Animal public void enjoy() System.out.println(貓叫聲貓叫聲.);public class Dog extends Animal public void enjoy() System.out.println(狗叫聲狗叫聲.);public class Test public static void main(String args) Animal animal; a
11、nimal = new Cat(); = 貓貓; animal.enjoy(); animal = new Dog(); = 狗狗; animal.enjoy(); Geometryperimeter Rectangle Height,widthComputePerimeterComputeArea Arc ComputePerimeter radius,angleComputePerimeter參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程ContentsContentsPart I Abstract Classes1Part II Interface2
12、Part III The Difference Between Them3Modifiers interface interfaceName constant abstract method Modifiers class className implements interfaceName extends Syntax如何理解接口:如何理解接口:(1)用類(lèi)來(lái)表示現(xiàn)實(shí)世界中的事物,用接口來(lái)表示事物所具備的行為能力;用類(lèi)來(lái)表示現(xiàn)實(shí)世界中的事物,用接口來(lái)表示事物所具備的行為能力;(2)接口里面定義的方法沒(méi)有任何代碼實(shí)現(xiàn),都是抽象的方法,因此接口就像一接口里面定義的方法沒(méi)有任何代碼實(shí)現(xiàn),都是抽象的方
13、法,因此接口就像一個(gè)規(guī)范一樣,規(guī)定了實(shí)現(xiàn)該接口的類(lèi)的具備怎樣的規(guī)則。個(gè)規(guī)范一樣,規(guī)定了實(shí)現(xiàn)該接口的類(lèi)的具備怎樣的規(guī)則。public interface Runnable public final int minSpeed = 1; public void start(); public void run(); public void stop();public interface Runnable public static final int minSpeed = 1; public abstract void start(); public abstract void run(); pub
14、lic abstract void stop();equivalent Note I :Interfaces is a special abstract class. .public interface Runnable public final int minSpeed = 1; public void start() public void run() public void stop();Compile Error Car Runnable+start:void+run:void Eatable+eatRice:void Student+stop:void+eatVegetables:v
15、oidpublic interface Eatable public void eatRice(); public void eatVegetables();public class Student implements Eatable, Runnable public void start() public void run() public void stop() public void eatRice() public void eatVegetables public class Car implements Runnable public void start() public vo
16、id run() public void stop() Note II : Interface support the multiple inheritanceNote III: A interface can provide the service for many classes which have no relation.Note IV: A class can implement many interfaces which have no relation. public interface Runnable public final int minSpeed = 1; public
17、 void start(); public void run(); public void stop();multiple inheritance接口記憶竅門(mén):接口記憶竅門(mén):(1 1) 接口是一種特殊的抽象類(lèi)接口是一種特殊的抽象類(lèi)(2 2) 接口可以實(shí)現(xiàn)多重繼承接口可以實(shí)現(xiàn)多重繼承(3 3) 多個(gè)無(wú)關(guān)的類(lèi)可以實(shí)現(xiàn)同一接口多個(gè)無(wú)關(guān)的類(lèi)可以實(shí)現(xiàn)同一接口 (4 4) 一個(gè)類(lèi)可以實(shí)現(xiàn)多個(gè)無(wú)關(guān)的接口一個(gè)類(lèi)可以實(shí)現(xiàn)多個(gè)無(wú)關(guān)的接口 Studentstudy SingsingPopMusicsingRockandRoll PaintpaintOilPaintingpaintSketch Teacherteac
18、hCoursepublic interface Paint public void paintOilPainting(); public void paintSketch();public interface Sing public void singPopMusic(); public void singRockAndRoll(); public class Student implements Sing private String name; public Student(String name) = name; public void singPopMusic()
19、System.out.println(Student + name + is sing a pop Music); public void singRockAndRoll() System.out.println(Student + name + is sing a rock and roll); public void study() System.out.println(Student + name + is study); public class Teacher implements Sing, Paint private String name; public Teacher(Str
20、ing name) = name; public void singPopMusic() System.out.println(Teacher + name + is sing a pop Music); public void singRockAndRoll() System.out.println(Teacher +name +is sing a rock and roll) public void paintOilPainting() System.out.println(Teacher + name + is painting oil); public void p
21、aintSketch() System.out.println(Teacher + name + is painting sketch); public void teachCourse() System.out.println(Teacher + name + is teaching); public class Test public static void main(String args) Student s1 = new Student(Tom); s1.singPopMusic(); s1.singRockAndRoll(); Teacher s2 = new Teacher(Al
22、ice); s2.paintOilPainting(); s2.paintSketch(); Student Tomis sing a pop MusicStudent Tomis sing a rock and rollTeacher Aliceis painting oilTeacher Aliceis painting sketchThe Program Result Studentstudy SingsingPopMusicsingRockandRoll PaintpaintOilPaintingpaintSketch TeacherteachCourse Personnamebreathepublic class Teacher extends Person implements Sing,
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度保姆服務(wù)合同-針對(duì)老年人照護(hù)的專(zhuān)項(xiàng)服務(wù)條款
- 2025年度酒店與當(dāng)?shù)卣糜雾?xiàng)目合作合同
- 2025年農(nóng)業(yè)科技示范化肥農(nóng)藥農(nóng)膜供應(yīng)合作協(xié)議
- 2025年度借名購(gòu)車(chē)車(chē)輛維修保養(yǎng)服務(wù)協(xié)議
- 加入校團(tuán)委申請(qǐng)書(shū)
- 2020-2025年中國(guó)超聲波風(fēng)速風(fēng)向儀行業(yè)市場(chǎng)調(diào)查研究及投資前景預(yù)測(cè)報(bào)告
- 2025年度房地產(chǎn)貸款違約預(yù)警及處置合同范本
- 房屋鑒定申請(qǐng)書(shū)
- 2025年機(jī)制牛皮紙行業(yè)深度研究分析報(bào)告
- 2025年度惠州文化旅游項(xiàng)目投資合同范本
- 山東省青島市各縣區(qū)鄉(xiāng)鎮(zhèn)行政村村莊村名居民村民委員會(huì)明細(xì)及行政區(qū)劃代碼
- 《鉆井液用磺甲基酚醛樹(shù)脂技術(shù)要求》
- 數(shù)學(xué)-九宮數(shù)獨(dú)100題(附答案)
- 中國(guó)農(nóng)業(yè)發(fā)展銀行XX支行 關(guān)于綜合評(píng)價(jià)自評(píng)情況的報(bào)告
- 2010年宣武區(qū)第六屆中小學(xué)生地理知識(shí)競(jìng)賽題庫(kù)
- QC課題提高檢查井周邊壓實(shí)
- 應(yīng)征公民體格檢查表(征兵)
- ACL磁致伸縮液位計(jì)說(shuō)明書(shū)
- 優(yōu)秀教研組評(píng)比制度及實(shí)施細(xì)則
- 慈善祖師—太乙救苦天尊經(jīng)文選集拼音版
- 3建筑工程規(guī)劃放線(xiàn)、驗(yàn)線(xiàn)多測(cè)合一成果報(bào)告書(shū)
評(píng)論
0/150
提交評(píng)論