SlideShare a Scribd company logo
1 of 34
第三堂:
學習 Java 語法 (2)
物件導向程式設計
柯力中 Jason Ko
Jason的Android 快樂應用程式學習班
課程內容
1. 物件
2. 物件的繼承
3. (繼承的)多型
4. 抽象 Abstract class 與 method
5. 介面 Interface
物件
Jason的Android 快樂應用程式學習班
物件導向程式設計
Jason的Android 快樂應用程式學習班
• 我們生活在一個以物件理解的世界 ex. 一個人, 兩
條狗, 一個電鍋…等(人.狗.電鍋都是物件的概念)
• class 是物件的模板, 分為型態與行為兩部分
• 型態 => instance variable
• 行為(方法) => method
(.java 檔)
class
variable
method
物件範例 — Dog class
Dog
型態:
instance variable
行為:
method
String name;
int legs;
bark();
run();
物件練習 — Person class
Person
型態:
instance variable
行為:
method
String name;
int fingers;
int foots;
run();
jump();
speak();
物件
使用物件
輸出結果
小試身手:物件實作
Jason的Android 快樂應用程式學習班
1. 請依照以下規格做出 Dog 跟 Cat
class
Dog
String name;
int legs;
eat();
sleep();
Cat
String name;
int legs;
eat();
sleep();
繼承
Jason的Android 快樂應用程式學習班
繼承(inheritance)
Jason的Android 快樂應用程式學習班
• 當有許多相似物件時, 要一個個寫就顯得費事,
可以透過物件的繼承減少重複的程式
Dog Cat
Animal
bark();
run();
繼承的方法
Jason的Android 快樂應用程式學習班
繼承自... 母 class
繼承實作 part 1: Animal class
Jason的Android 快樂應用程式學習班
Jason的Android 快樂應用程式學習班
繼承實作 part 2 : Dog class 和 Cat class
繼承注意事項
• 如果有相同方法, 子類別(物件)會覆蓋父類別(物件)
• 使用 super 呼叫父類別成員變數或方法
Jason的Android 快樂應用程式學習班
繼承練習
Jason的Android 快樂應用程式學習班
題目:
動物家族 cat, dog, bird 都會 eat() 跟 sleep(),
此外 cat 會 mew(), dog 會 bark(), bird 會 fly(),
請依此設定建構出 Animal, Cat, Dog, Bird 的
classes
猜猜看
• Animal 有 eat(), Cat 有 eat(), 那程式會
呼叫哪一個 ?
Animal
Cateat();
eat();
多型
• 多型就是意味著物件有多種型式, 看你是
指到了哪個物件
Animal
Cateat();
eat();
強制型別轉換
Animal mCat = new Cat();
請問這樣可以嗎?
Jason的Android 快樂應用程式學習班
這樣呢?
mCat.mew();
可以
不可以, Animal 沒有此方法
要這樣, 型別轉換
Cat theCat = (Cat) mCat;
theCat.mew();
強制型別轉換說明
Jason的Android 快樂應用程式學習班
Animal
Cat
mCat
(Cat)theCat
Heap 記憶體空間
mCat 只指向 Animal 物件, theCat 指向 Cat 物件
檢查物件
• 要檢查物件, 用 isinstanceof
if (o isinstance of Dog) {
Dog d = (Dog) o;
}
程序開發 V.S 物件導向
Jason的Android 快樂應用程式學習班
程序開發 V.S 物件導向
題目:
請告訴我 Cat, Dog, Bird 的特殊技能?
(用程式寫出來)
答:
Cat 會 mew(); Dog 會 Bark(); Bird 會 fly();
程式請接下頁分曉~
程序開發 V.S 物件導向
程序開發
int x; // x= 1 表示 Cat, 2 表示 Dog, 3 表示
Bird
if (x == 1 ){
System.out.println(“喵喵喵”);
}else if( x == 2){
System.out.println(“旺旺”);
}else{
System.out.println(“飛飛”);
}
再來兩個動物, 就會記不住代號了!
程式也會很長 ~
程序開發 V.S 物件導向
物件導向 Cat mCat = new Cat();
mCat.mew();
Dog mDog = new Dog();
mDog.bark();
Bird mBird = new Bird();
mBird.fly();
呼叫動物(物件)本身的方法就好了!
再來兩個動物, 也不怕 ~ 科科~
抽象類別與介面
Jason的Android 快樂應用程式學習班
• 有些 class 我們希望只是抽象概念, 不希望被實作時, 使用抽象類
別
• 實作時, 只要在 class 前加上 abstract 就行了
• 換句話說, 抽象類別不能 new 物件
抽象類別 Abstract Class
Jason的Android 快樂應用程式學習班
抽象 method
• 所有的抽象方法都必須實作
• 用意是確保每個子類別都有這個 method
則子類必須實作 watch() 方法
抽象class 與 method實作
Jason的Android 快樂應用程式學習班
Abstract 的
method必須實作
介面 Interface 的使用時機
Jason的Android 快樂應用程式學習班
問 :
如果想給 Cat 跟 Dog 加上寵物的動作, 例如
walkWithHost(), kissHost(), 但 Bird 不想加上此
動作, 我們應該怎麼做?
答:
實作一個 Interface IPetting, 將 walkWithHost() 跟
kissHost() 寫入其中, 再由 Cat 跟 Dog 來
implement
• class 除了繼承之外, 還可以實作介面, 方法
為:
介面 Interface 的使用方法
Jason的Android 快樂應用程式學習班
class Cat extends Animal implements IPetting
使用 interface
的意思
所使用 interface
的名稱
介面 Interface 實作
Jason的Android 快樂應用程式學習班
• 因為 Java 不支援多重繼承, 要達到同時繼承多個父類別變數
或方法時, 我們會採用 implement 多個 Interface 的方法
• interface 可以當作是 100% 的 abstract class, interface 裏的
方法都需要實作
• Interface 可以 extends Interface
( interface 繼承 interface )
• Interface 的變數是 public final static
Interface 介面
Jason的Android 快樂應用程式學習班
參考
1. 深入淺出 Java 程式設計
http://www.books.com.tw/products/00103118
83
2. 最新 Java 7 程式語言
http://www.books.com.tw/products/00105883
25
Jason的Android 快樂應用程式學習班

More Related Content

Viewers also liked

第七堂 Youtube 播放 app (2)
第七堂 Youtube 播放 app (2)第七堂 Youtube 播放 app (2)
第七堂 Youtube 播放 app (2)力中 柯
 
第五堂 計算機App與網站瀏覽app
第五堂 計算機App與網站瀏覽app第五堂 計算機App與網站瀏覽app
第五堂 計算機App與網站瀏覽app力中 柯
 
第十堂 Android 5.0 lollipop
第十堂 Android 5.0 lollipop第十堂 Android 5.0 lollipop
第十堂 Android 5.0 lollipop力中 柯
 
第九堂 手機的資料保存
第九堂 手機的資料保存第九堂 手機的資料保存
第九堂 手機的資料保存力中 柯
 
第四堂 Android 的架構與開發工具
第四堂 Android 的架構與開發工具第四堂 Android 的架構與開發工具
第四堂 Android 的架構與開發工具力中 柯
 
Admob and android
Admob and androidAdmob and android
Admob and android力中 柯
 
第八堂 學習使用 Service
第八堂 學習使用 Service第八堂 學習使用 Service
第八堂 學習使用 Service力中 柯
 
第一堂 進入 Android 的世界 --- Jason 的 Android 應用程式快樂學習班
第一堂 進入 Android 的世界 --- Jason 的  Android 應用程式快樂學習班第一堂 進入 Android 的世界 --- Jason 的  Android 應用程式快樂學習班
第一堂 進入 Android 的世界 --- Jason 的 Android 應用程式快樂學習班力中 柯
 
第六堂 Youtube 播放 App (1)
第六堂 Youtube 播放 App (1)第六堂 Youtube 播放 App (1)
第六堂 Youtube 播放 App (1)力中 柯
 
Java 8 與 retrolambda
Java 8 與 retrolambdaJava 8 與 retrolambda
Java 8 與 retrolambdaJustin Lin
 
How I learn APP (2015)
How I learn APP (2015)How I learn APP (2015)
How I learn APP (2015)Ryan Chung
 
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 KEN studio
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
ECX2014 Pinkoi 網站使用者經驗設計的二三事
ECX2014 Pinkoi 網站使用者經驗設計的二三事ECX2014 Pinkoi 網站使用者經驗設計的二三事
ECX2014 Pinkoi 網站使用者經驗設計的二三事悠識學院
 
UX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesUX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesNed Potter
 

Viewers also liked (16)

第七堂 Youtube 播放 app (2)
第七堂 Youtube 播放 app (2)第七堂 Youtube 播放 app (2)
第七堂 Youtube 播放 app (2)
 
第五堂 計算機App與網站瀏覽app
第五堂 計算機App與網站瀏覽app第五堂 計算機App與網站瀏覽app
第五堂 計算機App與網站瀏覽app
 
第十堂 Android 5.0 lollipop
第十堂 Android 5.0 lollipop第十堂 Android 5.0 lollipop
第十堂 Android 5.0 lollipop
 
第九堂 手機的資料保存
第九堂 手機的資料保存第九堂 手機的資料保存
第九堂 手機的資料保存
 
第四堂 Android 的架構與開發工具
第四堂 Android 的架構與開發工具第四堂 Android 的架構與開發工具
第四堂 Android 的架構與開發工具
 
Admob and android
Admob and androidAdmob and android
Admob and android
 
第八堂 學習使用 Service
第八堂 學習使用 Service第八堂 學習使用 Service
第八堂 學習使用 Service
 
第一堂 進入 Android 的世界 --- Jason 的 Android 應用程式快樂學習班
第一堂 進入 Android 的世界 --- Jason 的  Android 應用程式快樂學習班第一堂 進入 Android 的世界 --- Jason 的  Android 應用程式快樂學習班
第一堂 進入 Android 的世界 --- Jason 的 Android 應用程式快樂學習班
 
第六堂 Youtube 播放 App (1)
第六堂 Youtube 播放 App (1)第六堂 Youtube 播放 App (1)
第六堂 Youtube 播放 App (1)
 
Java 8 與 retrolambda
Java 8 與 retrolambdaJava 8 與 retrolambda
Java 8 與 retrolambda
 
How A Compiler Works: GNU Toolchain
How A Compiler Works: GNU ToolchainHow A Compiler Works: GNU Toolchain
How A Compiler Works: GNU Toolchain
 
How I learn APP (2015)
How I learn APP (2015)How I learn APP (2015)
How I learn APP (2015)
 
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
阿肯 - UI & UX 從核心資訊與路徑設計談起 (2015/01/27)
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
ECX2014 Pinkoi 網站使用者經驗設計的二三事
ECX2014 Pinkoi 網站使用者經驗設計的二三事ECX2014 Pinkoi 網站使用者經驗設計的二三事
ECX2014 Pinkoi 網站使用者經驗設計的二三事
 
UX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesUX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and Archives
 

第三堂 Java 語法 (2) 物件導向程式設計