ข้อมูลระดับ instance และระดับคลาส

การเขียนโปรแกรมเชิงวัตถุใน Python

Alex Yarosh

Content Quality Analyst @ DataCamp

หลักการพื้นฐานของ OOP

 

Inheritance:

  • ขยายฟังก์ชันการทำงานของโค้ดที่มีอยู่

Polymorphism:

  • สร้าง interface แบบรวมศูนย์

Encapsulation:

  • รวมข้อมูลและเมธอดเข้าด้วยกัน
การเขียนโปรแกรมเชิงวัตถุใน Python

ข้อมูลระดับ instance

class Employee:
    def __init__(self, name, salary):
       self.name = name
       self.salary = salary

emp1 = Employee("Teo Mille", 50000) 
emp2 = Employee("Marta Popov", 65000)
  • name, salary คือ instance attributes
  • self ผูกกับ instance
การเขียนโปรแกรมเชิงวัตถุใน Python

ข้อมูลระดับคลาส

  • ข้อมูลที่ใช้ร่วมกันในทุก instance ของคลาส
  • กำหนด class attributes ในส่วน body ของ class
class MyClass:
    # Define a class attribute
    CLASS_ATTR_NAME = attr_value


  • คือ "ตัวแปร global" ภายในคลาส
การเขียนโปรแกรมเชิงวัตถุใน Python

ข้อมูลระดับคลาส

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    #<--- no self. 

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
  • MIN_SALARY ใช้ร่วมกันในทุก instance
  • ไม่ใช้ self ในการ กำหนด class attribute
  • ใช้ ClassName.ATTR_NAME เพื่อ เข้าถึง ค่า class attribute
การเขียนโปรแกรมเชิงวัตถุใน Python

ข้อมูลระดับคลาส

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
emp1 = Employee("TBD", 40000)
print(emp1.MIN_SALARY)
30000
emp2 = Employee("TBD", 60000)
print(emp2.MIN_SALARY)
30000
การเขียนโปรแกรมเชิงวัตถุใน Python

เหตุใดจึงใช้ class attributes?

ค่าคงที่ global ที่เกี่ยวข้องกับคลาส

 

  • ค่าต่ำสุด/สูงสุดของ attributes
  • ค่าและค่าคงที่ที่ใช้บ่อย เช่น pi สำหรับคลาส Circle
  • ...
การเขียนโปรแกรมเชิงวัตถุใน Python

Class methods

  • เมธอดถูก "ใช้ร่วมกัน" อยู่แล้ว: โค้ดเดียวกันสำหรับทุก instance
  • class methods ไม่สามารถใช้ข้อมูลระดับ instance ได้
class MyClass:

  @classmethod                         # <---use decorator to declare a class method

def my_awesome_method(cls, args...): # <---cls argument refers to the class # Do stuff here # Can't use any instance attributes :(
MyClass.my_awesome_method(args...)
การเขียนโปรแกรมเชิงวัตถุใน Python

Alternative constructors

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY
  • มี __init__() ได้เพียงตัวเดียว

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)

  • ใช้ class methods เพื่อสร้างออบเจกต์
  • ใช้ return เพื่อคืนค่าออบเจกต์
  • cls(...) จะเรียก __init__(...)
การเขียนโปรแกรมเชิงวัตถุใน Python

Alternative constructors

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)        

ไฟล์ข้อความที่มีหนึ่งบรรทัดซึ่งระบุชื่อพนักงาน

# Create an employee without calling Employee()
emp = Employee.from_file("employee_data.txt")
type(emp)
__main__.Employee
การเขียนโปรแกรมเชิงวัตถุใน Python

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

การเขียนโปรแกรมเชิงวัตถุใน Python

Preparing Video For Download...