OOP - CLASS

Aj.Krit Th.

ฺBrand: Lamborghini

Color: Red

Price: 24,000,000

ฺBrand: Toyota

Color: White

Price: 1,400,000

ฺBrand: Volkswagen

Color: Yellow

Price: 1,500,000

Class

Objects (Instaces)

Attributes

Python OOP

CLASS

class MyClass:
  x = 5

สร้าง Object จาก Class (Instance)

p1 = MyClass()
print(p1.x)

สร้าง Class

Python OOP

CLASS

del p1

สร้าง Object จาก Class (Instance)

p1 = MyClass()
p2 = MyClass()
p3 = MyClass()

print(p1.x)
print(p2.x)
print(p3.x)

ลบ Object

Python OOP

CLASS: __init__()

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

p1 = Person("อ.กฤษณ์", 26)

print(p1.name)
print(p1.age)

__init__() (Method) จะทำงานอัตโนมัติเมื่อ Class ถูกใช้งาน

 

Method (เมธ็อด) ใน OOP เปรียบเสมือนกับ ฟังก์ชั่น ที่เรียกใช้งานได้ตามต้องการ

Python OOP

CLASS: self

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

  def greet(self):
    print("Hello, my name is " + self.name)

p1 = Person("Emil", 25)
p1.greet()

พารามิเตอร์ self คือ ตัวอ้างอิง (reference) ไปยังอ็อบเจกต์ปัจจุบันของคลาสนั้น

Python OOP

CLASS: self

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

  def printname(self):
    print(self.name)

p1 = Person("ม.พชร")
p2 = Person("ม.สรรค์พงษ์")

p1.printname()
p2.printname()

ทำไมต้องใช้ self ?

 

เพราะในคลาสสามารถมี
อ็อบเจกต์ได้หลายตัว
ถ้าไม่ใช้ self โปรแกรมจะไม่สามารถระบุได้ว่ากำลังอ้างถึงข้อมูล
ของอ็อบเจกต์ใดอยู่

Python OOP

CLASS: Properties

class Car:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

car1 = Car("Toyota", "Cross")

print(car1.brand)
print(car1.model)

Properties คือ ตัวแปรที่เป็นของคลาส ใช้เก็บข้อมูลให้กับอ็อบเจกต์แต่ละตัวที่ถูกสร้างขึ้นจากคลาสนั้น

 

คลาส = แม่แบบ

อ็อบเจกต์ = สิ่งที่ถูกสร้าง
จากแม่แบบ

Properties = จะเก็บค่าของ
อ็อบเจกต์แต่ละตัวแยกจากกัน

Python OOP

CLASS: Properties แก้ไขข้อมูล

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

p1 = Person("อ.กฤษณ์", 26)
print(p1.age)

p1.age = 27
print(p1.age)

Python OOP

CLASS: Properties ลบข้อมูล

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

p1 = Person("ศรีวรการ", 52)

del p1.age

print(p1.name)

Python OOP

CLASS: Properties CLASS vs OBJECT

class Person:
  workplace = "ศรีวรการ" # Class property

  def __init__(self, name):
    self.name = name # Instance property

p1 = Person("กฤษณ์")
p2 = Person("พชร")

print(p1.name)
print(p2.name)
print(p1.workplace)
print(p2.workplace)

 

instance property = ส่วนตัว class property = ส่วนรวม

Python OOP

CLASS: Properties CLASS vs OBJECT

class Person:
  lastname = ""

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

p1 = Person("สรรค์พงษ์")
p2 = Person("พชร")

Person.lastname = "วรศิลป์"

print(p1.lastname)
print(p2.lastname)

Python OOP

CLASS: Properties CLASS vs OBJECT

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

p1 = Person("กฤษณ์")

p1.age = 26
p1.city = "กรุงเทพ"

print(p1.name)
print(p1.age)
print(p1.city)

Python OOP

CLASS: Method (เมธ็อด)

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

  def greet(self):
    print("สวัสดี, ฉันชื่อ " + self.name)

p1 = Person("กฤษณ์")
p1.greet()

ฟังก์ชันที่เป็นของคลาส ใช้กำหนดพฤติกรรม (behavior) ของอ็อบเจกต์ที่ถูกสร้างจากคลาสนั้น


Methods บอกว่าอ็อบเจกต์ ทำอะไรได้บ้าง

 

เมื่อสร้างอ็อบเจกต์จากคลาส อ็อบเจกต์นั้นจะใช้ methods เหล่านี้ได้

Python OOP

CLASS: Method with Parameters

class Calculator:
  def add(self, a, b):
    return a + b

  def multiply(self, a, b):
    return a * b

calc = Calculator()
print(calc.add(2, 6))
print(calc.multiply(12, 5))

สามารถรับพารามิเตอร์ได้เหมือนกับฟังก์ชันทั่วไป

Python OOP

CLASS: Method with Parameters

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

  def celebrate_birthday(self):
    self.age += 1
    print(f"สุขสันต์วันเกิด! ปีนี้อายุครบ {self.age} ขวบ")

p1 = Person("กฤษณ์",26)
p1.celebrate_birthday()
p1.celebrate_birthday()

Method สามารถเข้าใช้งานตัวแปร (Property) ได้

Python OOP

CLASS: Method __str__()

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

  def __str__(self):
    return f"{self.name} อายุ {self.age} ขวบ"

p1 = Person("กฤษณ์", 35)
print(p1)

เป็นเมธอดพิเศษ ที่ใช้กำหนดว่าจะแสดงผลอะไรออกมาเมื่อมีการพิมพ์ (print) จากอ็อบเจกต์โดยตรง

Python OOP

ลองศึกษาโค้ด . .

class Playlist:
  def __init__(self, name):
    self.name = name
    self.songs = []

  def add_song(self, song):
    self.songs.append(song)
    print(f"Added: {song}")

  def remove_song(self, song):
    if song in self.songs:
      self.songs.remove(song)
      print(f"Removed: {song}")

  def show_songs(self):
    print(f"Playlist '{self.name}':")
    for song in self.songs:
      print(f"- {song}")

my_playlist = Playlist("เพลงโปรด")
my_playlist.add_song("กลัวว่าฉันจะไม่เสียใจ")
my_playlist.add_song("ใจฉันตามเธอไป")
my_playlist.show_songs()

© Aj. Krit Th.

https://www.kritth.com

31910-2005-oop-202502-wk3-python-class

By Krit Th.

31910-2005-oop-202502-wk3-python-class

  • 27