物件導向程式設計基礎

Python 物件導向程式設計進階

Jake Roach

Data Engineer

定義類別

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.height = 0

# This invokes a call to __init__
john = Person("John Casey", 38)
  • 使用 class 關鍵字定義類別
  • __init__ 稱為建構子,建立新物件時會被呼叫
  • self 指向目前這個類別的實例
Python 物件導向程式設計進階

實例屬性

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
# Create an instance of Person
sarah = Person("Sarah Walker", 31)
sarah.age  # Retrieve the age instance attribute
  • 與某個類別物件相關聯
  • self.<attribute-name> 設定與存取
  • <object-name>.<attribute-name> 來取得
Python 物件導向程式設計進階

類別屬性

  • 隸屬於類別本身
  • 不需建立物件即可存取
  • 儲存所有物件都相同的資料
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Python 物件導向程式設計進階

實例方法

class Person:
  ...
  def introduce(self):
      print(f"Hello, my name is {self.name}")
chuck = Person("Chuck", 32)
chuck.introduce()  # Called on a Person object

$$

  • 需要已有該類別的物件才能呼叫
  • self 為第一個參數
Python 物件導向程式設計進階

類別方法

class Person:
  @classmethod
  def wake_up(cls):
      print("Time to start your day!")
# Calling a class method
Person.wake_up()

$$

  • @classmethod 修飾
  • 不需類別物件即可呼叫
Python 物件導向程式設計進階

繼承

繼承可在類別間重複使用程式碼。

  • 子類別(Employee)會繼承父類別(Person)的所有功能
  • 為「is-a」關係
  • 可再加入額外功能
    • 屬性
    • 方法
class Person:
  def __init__(self, name, age):
    self.name
    self.age = age

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, age, title):
    Person.__init__(self, name, age)
    self.title = title

  def change_position(self, new_title):
      self.title = new_title
Python 物件導向程式設計進階

繼承

lester = Employee("Lester", 26, "Technician")
lester.introduce()  # Inherited from Person
print(lester.title)
Hello, my name is Lester
Technician
lester.change_position("Cashier")
print(lester.title)
Cashier
Python 物件導向程式設計進階

super()

class Employee(Person):
  def __init__(self, name, age, title):
    # Uses name of the parent class
    Person.__init__(self, name, age)

    self.title = title
  ...
  • 類別名稱、__init__()

使用 super()

class Employee(Person):
  def __init__(self, name, age, title):
    # super(), instead of class name
    super().__init__(name, age)

    self.title = title
  ...
  • super()__init__()
  • 不需傳入 self
Python 物件導向程式設計進階

覆寫

子類以全新的方式實作從父類繼承的方法。

class Employee(Person):
  ...
  def introduce(self):
      print(f"""My name is {self.name},
        I am a {self.title}""")
lester = Employee("Lester", 26, "Technician")
lester.introduce()
My name is Lester, I am a Technician
Python 物件導向程式設計進階

多載

class Person:
  def __init__(self, name):
      self.name = name

  def __eq__(self, other):
      return self.name == other.name

$$

  • 為類別自訂 Python 運算子的行為
  • __eq__() 用來多載 ==

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Python 物件導向程式設計進階

一起來練習吧!

Python 物件導向程式設計進階

Preparing Video For Download...