9. 後來的我們
Solution
● 將程式碼變成 function (def)
● 變成黑盒子
Good
● 只需要知道 function 名字就可以
使用
def print_add_two_element():
first = input()
second = input()
print(first+second)
print_add_two_element()
9
Input Output
10. 後來的我們
Solution
● 將程式碼變成 function (def)
● 變成黑盒子
Good
● 只需要知道 function 名字就可以
使用
def print_add_two_element():
first = input()
second = input()
print(first+second)
print_add_two_element()
10
BUT 只能達到這樣嗎??
Input Output
24. Make it Move!!!!
class Car:
def __init__(self, name):
self.name = name
self.speed = 5
self.direction = 'straight'
def turn(self, direction):
self.direction = direction
def move(self):
print(f'{self.SPEED} pixels to {self.direction}')
>>> mycar = Car('kiwi')
>>> mycar.move()
5 pixels to Right
>>> mycar.turn('Right')
>>> mycar.move()
5 pixels to Right
建構子 (constructor)
依照 class 定義的藍圖,
建造實體可操作的物體
24
完成了最簡單的 Class 了
٩(๑❛ᴗ❛๑)۶
self - 代表目前的 class
● __init__
● attribute - self.attr
● method - self.func()
25. 25
超越 function 的功能 - Object Oriented Programming
架構想法
1. 設計可重覆製造的
a. class/instance
2. 加入記憶單元及動作
a. attribute/method
3. 彼此間 attribute 的互通
a. class attribute/instance attribute
26. 彼此間互通 - Attribute
Class Attribute
類別共通使用的變數
class Car:
MAX_SPEED = 5
Instance Attribute
實體化物件自己使用的
class Car:
def __init__(self, name):
self.name = name
class Dog:
kind = 'pomchi' # class attribute shared by all instances
def __init__(self, name):
self.name = name # instance attribute unique to each
instance
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind # shared by all dogs
'pomchi'
>>> e.kind # shared by all dogs
'pomchi'
>>> d.name # unique to d
'Fido'
>>> e.name # unique to e
'Buddy'
26
27. 小心使用可共享的 class attribute
Class attribute
● 可被相同 class 的修改
● 避免 Mutable object:
○ list / dict / set
class Dog:
tricks = [] # mistaken use of a class attribute
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks # unexpectedly shared by all dogs
['roll over', 'play dead']
27
d e
tricks
list
Dog
28. 28
超越 function 的功能 - Object Oriented Programming
架構
● [設計稿] Class
● [實體] Instance
● [記憶] Attribute
○ Class Attribute
○ Instance Attribute
● [動作] Method
75. [封裝][繼承] 妥善使用組合與繼承
不要為了 code reuse 而使用繼承 (is a) ,可考慮使用組合 (has a)
Composition
Inheritance
75
Civil aircraft IS a plane.
Fighter aircraft IS a plane.
Airport HAS lots of planes, like Civil
aircraft and Fighter aircraft
76. [繼承][多型] 抽象類別的簡介
Abstract Class 抽象類別
不可實體化,定義規定
● 需要會吃/上廁所/叫
Concrete Class 具體類別
必須滿足抽象類別的規定
● 狗:bark
● 貓:喵
Instance 實體
● 我的狗狗
Animal
Cat
Dog
76
92. Step.1 - 加減乘除
class Calculator:
def add(self, x, y):
self.x = x
self.y = y
a = self.x + self.y
return a
def subtract(self, x, y):
self.x = x
self.y = y
a = self.x - self.y
return a
def multiply(self, x, y):
self.x = x
self.y = y
a = self.x * self.y
return a
def divide(self, x, y):
self.x = x
self.y = y
if (y == 0):
a = "You can't divide by zero!"
else:
a = self.x / self.y 93
加入基本的加減乘除
96. Step1 - 分析需求
1. 導入類別繼承的概念,先定義 Car 所需的
功能
2. Car 類別具備的需求
a. 前進
b. 倒退
c. 轉方向
1. 定義此 Car 類別所具備的 method
a. move()
b. accelerate()
c. brake()
d. turn_left()
e. turn_right()
f. show()
Class CAR 的 method
● move: 根據目前的速度做移動
○ 正數:往前
○ 負數:往後
● accelerate: 速度增加 1
● brake:速度減少 1
○ 減為負則視為倒車
● turn_left: 往左轉
● turn_right: 往右轉
● show: 目前速度
97