Skip to content

บทที่ 4.1: คลาสและออบเจกต์ (Classes & Objects)

เอกสารนี้อธิบายถึงแนวคิดการเขียนโปรแกรมเชิงวัตถุ (Object-Oriented Programming: OOP) การสร้างคลาส (Class) และออบเจกต์ (Object) การใช้งาน Method __init__ ตัวแปร self รวมถึงความแตกต่างระหว่าง Instance Attribute และ Class Attribute


1. แนวคิดพื้นฐานเกี่ยวกับ OOP (OOP Concepts)

การเขียนโปรแกรมเชิงวัตถุ มองสิ่งต่างๆ ในโปรแกรมเป็น ออบเจกต์ (Object) ซึ่งรวบรวมทั้ง ข้อมูล (Attributes) และ การทำงาน (Methods) เข้าด้วยกัน

  • Class (คลาส): เป็นพิมพ์เขียว (Blueprint) หรือแม่แบบที่ใช้กำหนดโครงสร้างและพฤติกรรม
  • Object / Instance (ออบเจกต์): เป็นตัววัตถุจริงที่สร้างขึ้นมาจากพิมพ์เขียวของคลาส

2. การสร้าง Class และ Instance (Class & Object Creation)

2.1 โครงสร้างคลาส และตัวแปร self

ตัวแปร self ทำหน้าที่เป็นตัวอ้างอิงชี้ไปยัง Instance ปัจจุบัน ที่กำลังถูกเรียกใช้งาน เพื่อให้สามารถเข้าถึง Attributes และ Methods ของ Instance นั้นๆ ได้

# การสร้าง Class
class Dog:
    # Constructor Method (ทำงานอัตโนมัติเมื่อสร้าง Object)
    def __init__(self, name, age):
        # Instance Attributes (ข้อมูลเฉพาะของแต่ละ Object)
        self.name = name
        self.age = age

    # Instance Method (พฤติกรรมของ Object)
    def bark(self):
        return f"{self.name} กำลังเห่า: โฮ่งๆ!"

# การสร้าง Object (Instantiation)
dog1 = Dog("โคล่า", 3)
dog2 = Dog("มอมแมม", 5)

# การเข้าถึง Attributes และ Methods
print(dog1.name)    # Output: โคล่า
print(dog2.bark())  # Output: มอมแมม กำลังเห่า: โฮ่งๆ!

3. Class Attributes vs Instance Attributes

ประเภท ขอบเขตการทำงาน การสร้าง/เข้าถึง
Instance Attribute ข้อมูลประจำตัวของแต่ละ Instance แยกกันอิสระ ประกาศภายใน __init__ โดยใช้ self.variable
Class Attribute ข้อมูลส่วนกลางที่แชร์ร่วมกันในทุก Instance ประกาศไว้ในคลาสโดยตรงนอก Method
class Employee:
    # Class Attribute (ทุก Instance ใช้ค่านี้นำร่วมกัน)
    min_salary = 15000
    employee_count = 0

    def __init__(self, name, salary):
        # Instance Attributes (แยกตามบุคคล)
        self.name = name
        self.salary = salary

        # อัปเดตค่า Class Attribute
        Employee.employee_count += 1

# สร้าง Object
emp1 = Employee("Alice", 30000)
emp2 = Employee("Bob", 25000)

print(emp1.salary)              # Output: 30000 (Instance Attribute)
print(emp2.salary)              # Output: 25000 (Instance Attribute)
print(Employee.employee_count)  # Output: 2 (Class Attribute)

4. การจัดการพฤติกรรมด้วย Instance Methods

Instance Method คือฟังก์ชันภายในคลาสที่รับ self เป็นพารามิเตอร์ตัวแรกเสมอ สามารถอ่านและแก้ไขข้อมูลภายใน Instance นั้นได้

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            print(f"ฝากเงิน {amount:,.2f} บาท สำเร็จ | ยอดคงเหลือ: {self.balance:,.2f} บาท")

    def withdraw(self, amount):
        if 0 < amount <= self.balance:
            self.balance -= amount
            print(f"ถอนเงิน {amount:,.2f} บาท สำเร็จ | ยอดคงเหลือ: {self.balance:,.2f} บาท")
        else:
            print("ยอดเงินในบัญชีไม่พอ หรือจำนวนเงินไม่ถูกต้อง")

# เรียกใช้งาน
account = BankAccount("John", 1000)
account.deposit(500)   # ยอดคงเหลือ: 1,500.00 บาท
account.withdraw(200)  # ยอดคงเหลือ: 1,300.00 บาท