พื้นฐานการเขียนโปรแกรมเชิงวัตถุ

Object-Oriented Programming ใน 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__ คือ constructor ซึ่งถูกเรียกเมื่อสร้างออบเจกต์ใหม่
  • self อ้างถึง instance ปัจจุบันของคลาส
Object-Oriented Programming ใน Python ระดับกลาง

Instance attributes

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>
Object-Oriented Programming ใน Python ระดับกลาง

Class attributes

  • ผูกกับคลาสโดยตรง
  • ดึงค่าได้โดยไม่ต้องสร้างออบเจกต์
  • เก็บข้อมูลที่ใช้ร่วมกันในทุกออบเจกต์ของคลาส
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Object-Oriented Programming ใน Python ระดับกลาง

Instance methods

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 เป็นพารามิเตอร์ตัวแรก
Object-Oriented Programming ใน Python ระดับกลาง

Class methods

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

$$

  • ใช้ decorator @classmethod
  • ไม่จำเป็นต้องมีออบเจกต์ของคลาสเพื่อเรียกใช้
Object-Oriented Programming ใน Python ระดับกลาง

การสืบทอด (Inheritance)

การสืบทอด (Inheritance) ช่วยให้นำโค้ดกลับมาใช้ซ้ำระหว่างคลาสได้

  • คลาสลูก (Employee) รับฟังก์ชันการทำงานทั้งหมดจากคลาสแม่ (Person)
  • ความสัมพันธ์แบบ "is-a"
  • สามารถเพิ่มฟังก์ชันใหม่ได้
    • Attributes
    • Methods
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
Object-Oriented Programming ใน Python ระดับกลาง

การสืบทอด (Inheritance)

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
Object-Oriented Programming ใน 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
Object-Oriented Programming ใน Python ระดับกลาง

การ Override

คลาสลูก override เมธอดที่รับสืบทอดมาในแบบใหม่

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
Object-Oriented Programming ใน Python ระดับกลาง

การ Overload

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

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

$$

  • ปรับแต่งพฤติกรรมของ operator ใน Python สำหรับคลาส
  • __eq__() ใช้สำหรับ overload ==

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Object-Oriented Programming ใน Python ระดับกลาง

มาฝึกกันเถอะ!

Object-Oriented Programming ใน Python ระดับกลาง

Preparing Video For Download...