物件導向程式設計
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code.
Python 的所有資料型別皆為物件,也允許開發者創造自己的物件。
由於這種物件的概念易於擴充、維護與重複使用,被現代多數的程式語言所採用,也適合用來開發大型的專案。
什麼是物件?類別?
物件(Object)= 真實存在的任何東西
An object is the basic building block of OOPs.
It can be considered a real-life entity having a state and behaviour.
- 每個物件 可以擁有(代表不強制一定要有)
屬性(properties)
與行為(behaviors)
,而且這些物件可以 在程式中相互傳遞。
類別(Class)= 抽象的藍圖
A Class is like an object constructor, or a blueprint for creating objects.
Step1:建立類別(Class)
class [ClassName]:
# Class attribute
class_attribute = default_class_attribute
# Instance attributes (e.g. name, age)
def __init__(self[, parameters]):
self.parameter = parameter
# Instance method
def instance_method1(self[, parameters]):
[statement]
class Dog:
eye = 2
mouth = 1
def __init__(self, name, age):
self.species = '柴犬'
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
- 定義
ClassName
時,習慣以 首字母大寫 的表示法書寫。 - __init__() = 建構式
- 建構函式,實例化物件時會呼叫它!實例化物件的初始化設定
- 在「建構式(Constructor)」中 初始化物件(Object) 的 屬性值(Attribute)
- self
- 代表 實例化物件(Instance) 本身,即 新建立的實體物件。
小練習
class Dog:
eye = 2
mouth = 1
def __init__(self, name, age):
self.species = '柴犬'
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
miles = Dog("Miles", 5)
print(miles.eye)
print(miles.species)
miles.species = '吉娃娃'
print(miles.species)
print(miles.description())
print(miles.speak('Woof'))
Step2:實例化 (Instantiate)
實例化 = 生成 物件/實例(Object / Instance)
一個 class 經過「實例化 (Instantiate)」就能創建出多個 Objects。
承上【 class Dog 範例 】,說明如下:
class Dog:
eye = 2
mouth = 1
def __init__(self, name, age):
self.species = '柴犬'
self.name = name
self.age = age
def __str__(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
# 實例化1
miles = Dog("Miles", 5)
print(miles) #'Miles is 5 years old'
print(miles.eye) #2
print(miles.speak("bark")) #'Miles says bark'
# 實例化2
cute = Dog("Cute", 2)
cute.species = '吉娃娃'
print(cute) #'Cute is 2 years old'
print(cute.mouth) #1
print(cute.species) #'吉娃娃'
print(cute.speak("汪汪")) #'Cute says 汪汪'
Instance attribute
- 定義 隨不同實例而異的屬性
- e.g.
__init__()
:定義 所有 Dog 物件 都必須具備 的實例屬性。- 即 每當實例化一隻狗時,
__init__()
內所定義的就是每隻狗 一開始的初始狀態。
Instance methods
__str__(self)
:印出該 Instance 的相關訊息與狀態(Pythonic 寫法)- 盡量不要使用 description()
繼承 (Inheritance)
class Dog:
eye = 2
mouth = 1
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
class Samoyed(Dog):
def __init__(self, name, age): # 初始化時給定的屬性(name, age)
super().__init__(name, age) # 使用 super() 繼承 Dog __init__ 裡的 name, age 屬性
self.species = '薩摩耶'
def attack(self):
return f"{self.name} 使出咬人攻擊"
# 實例化3
ss = Samoyed("颯颯", 7)
print(ss.species)
print(ss)
print(ss.eye)
print(ss.attack())