![chapter 10 Abstract Classes and Interfaces_第1頁](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頁](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頁](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頁](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頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d2/8d1566f1-ff87-487c-9dc4-1f71a5a4b2d25.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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抽象類記憶竅門:抽象類記憶竅門:(1)如果一個方法用)如果一個方法用abstract修飾,它就是抽象方法;一個類用修飾,它就是抽象方法;一個類用abstract修飾,它就是抽象類;修飾,它就是抽象類;(2)如果一個類中包含抽象方法,則該類是抽象類;抽象類里可以沒)如果一個類中包含抽象方法,則該類是抽象類;抽象類里可以沒有抽象方法。有抽象方法。(3)抽象方法沒有任何代碼實現(xiàn))
9、抽象方法沒有任何代碼實現(xiàn)(4)抽象類必須被繼承,抽象方法必須被子類重寫;這一點和)抽象類必須被繼承,抽象方法必須被子類重寫;這一點和final類和類和final方法不同,方法不同,final類不能被繼承,類不能被繼承,final方法不能被重寫方法不能被重寫(5)不能從抽象類生成實例。)不能從抽象類生成實例。 AnimalnameenjoyliveOnEarth Catenjoy Dogenjoy參見實驗指導書例程參見實驗指導書例程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參見實驗指導書例程參見實驗指導書例程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)用類來表示現(xiàn)實世界中的事物,用接口來表示事物所具備的行為能力;用類來表示現(xiàn)實世界中的事物,用接口來表示事物所具備的行為能力;(2)接口里面定義的方法沒有任何代碼實現(xiàn),都是抽象的方法,因此接口就像一接口里面定義的方法沒有任何代碼實現(xiàn),都是抽象的方
13、法,因此接口就像一個規(guī)范一樣,規(guī)定了實現(xiàn)該接口的類的具備怎樣的規(guī)則。個規(guī)范一樣,規(guī)定了實現(xiàn)該接口的類的具備怎樣的規(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接口記憶竅門:接口記憶竅門:(1 1) 接口是一種特殊的抽象類接口是一種特殊的抽象類(2 2) 接口可以實現(xiàn)多重繼承接口可以實現(xiàn)多重繼承(3 3) 多個無關的類可以實現(xiàn)同一接口多個無關的類可以實現(xiàn)同一接口 (4 4) 一個類可以實現(xiàn)多個無關的接口一個類可以實現(xià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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人購車訂金合同范例
- 公寓分租合同范例
- 個人裝修施工合同范本
- 北京月租房合同范例
- 加盟合同與買賣合同范例
- 出資分紅股合同范例
- 獸藥勞動合同范本
- 醫(yī)療專家聘用合同范例
- 人參訂購合同范本
- 劣質土地流轉合同范本
- 三對三籃球賽記錄表
- 礦山電工知識點講解
- 物業(yè)公司服務質量檢查流程
- 中國心胸外科的歷史和現(xiàn)狀
- 人教版9年級全一冊英語單詞表
- 三門峽水利工程案例分析工程倫理
- 中國旅游地理區(qū)劃-京津冀旅游區(qū)
- “1+X”證書制度試點職業(yè)技能等級證書全名錄
- 《社會主義市場經濟理論(第三版)》第八章社會主義市場經濟調控論
- 交流伺服系統(tǒng)常見故障及處理分解課件
- 水土保持單元工程質量評定表
評論
0/150
提交評論