Python การเปลี่ยนแปลง Object Properties

การเปลี่ยนแปลงคุณสมบัติของวัตถุ (Object Properties) ใน Python คือกระบวนการที่เราสามารถปรับเปลี่ยนค่า หรือกำหนดค่าของคุณสมบัติต่าง ๆ ของวัตถุที่ถูกสร้างจากคลาสได้ ซึ่งคุณสมบัติของวัตถุใน Python เป็นข้อมูลที่เก็บอยู่ในลักษณะตัวแปร (variable) ของแต่ละ object โดยการเปลี่ยนแปลงสามารถทำได้ทั้งภายในและภายนอกของคลาส

การเปลี่ยนแปลง Object Properties ภายในคลาส

การเปลี่ยนแปลงคุณสมบัติของวัตถุภายในคลาสสามารถทำได้ผ่านเมธอดที่ถูกนิยามภายในคลาสนั้น ๆ โดยใช้ self เพื่อเข้าถึงคุณสมบัติของวัตถุ

ตัวอย่าง

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

    def paint(self, new_color):
        # เปลี่ยนแปลงคุณสมบัติ color ของ object
        self.color = new_color
        print(f"The car has been painted {self.color}.")

# สร้าง object ของ Car
my_car = Car("Toyota", "Red")

# เรียกใช้เมธอดเพื่อเปลี่ยนแปลงคุณสมบัติ color
my_car.paint("Blue")  # Output: The car has been painted Blue.

ในตัวอย่างนี้ เมธอด paint() ถูกใช้เพื่อเปลี่ยนแปลงคุณสมบัติ color ของวัตถุ my_car โดยการกำหนดค่าใหม่ให้กับ self.color

การเปลี่ยนแปลง Object Properties ภายนอกคลาส

นอกจากการเปลี่ยนแปลงคุณสมบัติภายในคลาสแล้ว gikยังสามารถเข้าถึงและเปลี่ยนแปลงคุณสมบัติของวัตถุจากภายนอกคลาสได้โดยตรง

ตัวอย่าง

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

# สร้าง object ของ Car
my_car = Car("Honda", "Black")

# เปลี่ยนแปลงคุณสมบัติ color จากภายนอกคลาส
my_car.color = "White"
print(f"The car color is now {my_car.color}.")  # Output: The car color is now White.

ในตัวอย่างนี้ เราเข้าถึงคุณสมบัติ color ของวัตถุ my_car จากภายนอกคลาสและกำหนดค่าใหม่ให้เป็น "White"

การใช้ Getter และ Setter เพื่อจัดการกับ Object Properties

ในบางกรณี เราอาจต้องการควบคุมการเปลี่ยนแปลงคุณสมบัติของวัตถุให้เป็นไปตามกฎเกณฑ์หรือเงื่อนไขบางอย่าง เราสามารถใช้ getter และ setter เพื่อควบคุมการเข้าถึงและการเปลี่ยนแปลงคุณสมบัติได้

ตัวอย่าง

Python
class Car:
    def __init__(self, brand, color):
        self._brand = brand  # ใช้ _ นำหน้าเพื่อบอกว่าควรเป็น private
        self._color = color

    # Getter สำหรับ brand
    def get_brand(self):
        return self._brand

    # Setter สำหรับ brand
    def set_brand(self, new_brand):
        if isinstance(new_brand, str) and len(new_brand) > 0:
            self._brand = new_brand
        else:
            print("Invalid brand name.")

    # Getter สำหรับ color
    def get_color(self):
        return self._color

    # Setter สำหรับ color
    def set_color(self, new_color):
        if new_color in ["Red", "Blue", "Green", "Black", "White"]:
            self._color = new_color
        else:
            print("Invalid color.")

# สร้าง object ของ Car
my_car = Car("Ford", "Black")

# เรียกใช้ getter
print(my_car.get_brand())  # Output: Ford

# เรียกใช้ setter เพื่อเปลี่ยนแปลง brand
my_car.set_brand("Chevrolet")
print(my_car.get_brand())  # Output: Chevrolet

# เรียกใช้ setter เพื่อเปลี่ยนแปลง color
my_car.set_color("Blue")
print(my_car.get_color())  # Output: Blue

ในตัวอย่างนี้ เราใช้ getter (get_brand() และ get_color()) เพื่อเข้าถึงคุณสมบัติ และ setter (set_brand() และ set_color()) เพื่อกำหนดเงื่อนไขสำหรับการเปลี่ยนแปลงคุณสมบัตินั้น ๆ

การใช้ Property Decorators ใน Python

Python ยังมีฟีเจอร์ที่ช่วยให้การสร้าง getter และ setter สะดวกมากขึ้น โดยใช้ property decorators (@property) เพื่อทำให้โค้ดอ่านง่ายขึ้น

ตัวอย่าง

Python
class Car:
    def __init__(self, brand, color):
        self._brand = brand
        self._color = color

    # Property สำหรับ brand
    @property
    def brand(self):
        return self._brand

    @brand.setter
    def brand(self, new_brand):
        if isinstance(new_brand, str) and len(new_brand) > 0:
            self._brand = new_brand
        else:
            print("Invalid brand name.")

    # Property สำหรับ color
    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, new_color):
        if new_color in ["Red", "Blue", "Green", "Black", "White"]:
            self._color = new_color
        else:
            print("Invalid color.")

# สร้าง object ของ Car
my_car = Car("Mazda", "Red")

# ใช้งาน property เพื่อเข้าถึงและเปลี่ยนแปลงคุณสมบัติ
print(my_car.brand)  # Output: Mazda
my_car.brand = "Tesla"
print(my_car.brand)  # Output: Tesla

print(my_car.color)  # Output: Red
my_car.color = "Green"
print(my_car.color)  # Output: Green

ในตัวอย่างนี้ @property และ @brand.setter ทำให้เราสามารถเข้าถึงคุณสมบัติ brand และ color ของวัตถุได้อย่างสะดวกโดยไม่ต้องเรียกเมธอด getter และ setter แบบเดิม

สรุป

  • การเปลี่ยนแปลงคุณสมบัติของวัตถุสามารถทำได้ทั้งภายในคลาสผ่านเมธอดและภายนอกคลาสโดยตรง
  • การใช้ getter และ setter ช่วยในการควบคุมการเข้าถึงและเปลี่ยนแปลงคุณสมบัติของวัตถุ
  • Property decorators ช่วยให้การใช้งาน getter และ setter ง่ายขึ้น
แชร์เรื่องนี้