0Pricing
Python Academy · บทเรียน

พหุสัณฐาน

สำรวจว่าพหุสัณฐานช่วยให้โค้ดยืดหยุ่นและทำงานแบบไดนามิกได้อย่างไร

พหุสัณฐาน เป็นบทเรียน Python Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 6 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Python Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Python Academy มีบทเรียนทั้งหมด 6 บทเรียน

บทนำสู่พหุสัณฐาน

บทนำสู่พหุสัณฐาน

พหุสัณฐานเป็นแนวคิดสำคัญในการเขียนโปรแกรมเชิงวัตถุ (OOP) ซึ่งช่วยให้ออบเจ็กต์จากคลาสต่าง ๆ สามารถจัดการเสมือนเป็นออบเจ็กต์ของคลาสฐานร่วมกันได้ แนวคิดนี้ช่วยให้โค้ดยืดหยุ่นและนำกลับมาใช้ใหม่ได้

ในบทเรียนนี้ คุณจะได้เรียนรู้วิธีนำพหุสัณฐานไปใช้ในไพธอนโดยใช้เมธอดและคลาส

พหุสัณฐาน — ภาพประกอบ 1

พหุสัณฐานคืออะไร

พหุสัณฐานคืออะไร

พหุสัณฐานช่วยให้เมธอดในคลาสต่าง ๆ ใช้ชื่อเดียวกันได้ในขณะที่มีพฤติกรรมแตกต่างกัน ทำให้โค้ดยืดหยุ่นและต่อยอดได้ง่ายขึ้น

# Defining a superclass
class Animal:
    # Method in the superclass
    def speak(self):
        print("Animal speaks")  # Generic message for all animals

# Defining a subclass that overrides the speak method
class Dog(Animal):
    def speak(self):
        print("Dog barks")  # Specific behavior for Dog

พหุสัณฐานด้วยเมธอด

พหุสัณฐานด้วยเมธอด

เมธอดที่มีชื่อเดียวกันในคลาสต่าง ๆ สามารถมีการทำงานที่แตกต่างกันได้

# Another subclass with its own implementation of the speak method
class Cat(Animal):
    def speak(self):
        print("Cat meows")  # Specific behavior for Cat

# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal

def animal_sound(animal):
    animal.speak()  # Calls the appropriate speak method based on the object type

# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()

# Calling the function with different objects
animal_sound(dog)  # Outputs: Dog barks
animal_sound(cat)  # Outputs: Cat meows

ตัวอย่างพหุสัณฐานด้วยเมธอด

พหุสัณฐานด้วยเมธอด

เมธอดที่มีชื่อเดียวกันในคลาสต่าง ๆ สามารถมีการทำงานที่แตกต่างกันได้

# Another subclass with its own implementation of the speak method
class Cat(Animal):
    def speak(self):
        print("Cat meows")  # Specific behavior for Cat

# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal

def animal_sound(animal):
    animal.speak()  # Calls the appropriate speak method based on the object type

# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()

# Calling the function with different objects
animal_sound(dog)  # Outputs: Dog barks
animal_sound(cat)  # Outputs: Cat meows

พหุสัณฐานด้วยฟังก์ชัน

พหุสัณฐานด้วยฟังก์ชัน

พหุสัณฐานช่วยให้ฟังก์ชันเดียวกันทำงานกับออบเจ็กต์ประเภทต่าง ๆ ได้

# Defining two unrelated classes
class Bird:
    def fly(self):
        print("Bird flies")  # Behavior specific to birds

class Plane:
    def fly(self):
        print("Plane flies")  # Behavior specific to planes

# Function demonstrating polymorphism
# It can accept objects of both Bird and Plane

def flying_object(obj):
    obj.fly()  # Calls the appropriate fly method based on the object type

# Creating objects of Bird and Plane
bird = Bird()
plane = Plane()

# Calling the function with different objects
flying_object(bird)  # Outputs: Bird flies
flying_object(plane)  # Outputs: Plane flies

พหุสัณฐานด้วยการสืบทอด

พหุสัณฐานด้วยการสืบทอด

พหุสัณฐานมักเกิดขึ้นผ่านการสืบทอด โดยคลาสย่อยเขียนทับเมธอดจากคลาสฐาน

# Superclass representing a generic vehicle
class Vehicle:
    def start(self):
        print("Vehicle starts")  # Generic behavior for all vehicles

# Subclass representing a specific type of vehicle
class Car(Vehicle):
    def start(self):
        print("Car starts")  # Specific behavior for cars

# Subclass representing another specific type of vehicle
class Bike(Vehicle):
    def start(self):
        print("Bike starts")  # Specific behavior for bikes

# Function demonstrating polymorphism
# It can accept objects of any subclass of Vehicle

def start_vehicle(vehicle):
    vehicle.start()  # Calls the appropriate start method based on the object type

# Creating objects of Car and Bike
car = Car()
bike = Bike()

# Calling the function with different objects
start_vehicle(car)  # Outputs: Car starts
start_vehicle(bike)  # Outputs: Bike starts

ข้อดีของพหุสัณฐาน

ข้อดีของพหุสัณฐาน

พหุสัณฐานมีประโยชน์หลายประการ:

  • ช่วยให้โค้ดนำกลับมาใช้ใหม่ได้และมีความยืดหยุ่น
  • ช่วยปรับปรุงความอ่านง่ายและการจัดระเบียบของโค้ด
  • ส่งเสริมการใช้อินเทอร์เฟซร่วมกัน

ตัวอย่างจากโลกจริง

ตัวอย่างจากโลกจริง

ต่อไปนี้คือตัวอย่างพหุสัณฐานจากโลกจริง

# Real-world example of polymorphism
class Developer:
    def work(self):
        print("Writing code")  # Specific task for developers

class Designer:
    def work(self):
        print("Designing user interfaces")  # Specific task for designers

# Function demonstrating polymorphism
# It can accept objects of both Developer and Designer

def workday(employee):
    employee.work()  # Calls the appropriate work method based on the object type

# Creating objects of Developer and Designer
developer = Developer()
designer = Designer()

# Calling the function with different objects
workday(developer)  # Outputs: Writing code
workday(designer)  # Outputs: Designing user interfaces

ข้อผิดพลาดที่พบบ่อยเกี่ยวกับพหุสัณฐาน

ข้อผิดพลาดที่พบบ่อยเกี่ยวกับพหุสัณฐาน

ต่อไปนี้คือข้อผิดพลาดบางอย่างที่ควรหลีกเลี่ยง:

  • การลืมเขียนทับเมธอดในคลาสย่อยเมื่อจำเป็น
  • การสับสนลำดับการค้นหาเมธอด (MRO) ในการสืบทอดหลายลำดับ
  • การใช้พหุสัณฐานโดยไม่จำเป็นกับงานง่าย ๆ

เราเรียนรู้อะไรบ้าง

เราเรียนรู้อะไรบ้าง

ในบทเรียนนี้ คุณได้เรียนรู้:

  • พหุสัณฐานคืออะไรและทำงานอย่างไรในไพธอน
  • วิธีนำพหุสัณฐานไปใช้ด้วยเมธอดและการสืบทอด
  • ข้อดีของการใช้พหุสัณฐานในการเขียนโปรแกรมเชิงวัตถุ

ทำได้ดีมาก ไปยังหัวข้อถัดไปกัน

พหุสัณฐาน — ภาพประกอบ 12

คำถามที่พบบ่อย

บทเรียน “พหุสัณฐาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พหุสัณฐาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Python Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Python Academy มีบทเรียนทั้งหมด 6 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พหุสัณฐาน”

สำรวจว่าพหุสัณฐานช่วยให้โค้ดยืดหยุ่นและทำงานแบบไดนามิกได้อย่างไร คุณปฏิบัติ Python Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Python Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Python Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 6 บทเรียน

บทเรียน “พหุสัณฐาน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Python Academy นี้ได้ไหม

ได้ บทเรียน Python Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คลาสและอ็อบเจ็กต์
  2. แอตทริบิวต์และเมธอด
  3. การสืบทอด
  4. พหุสัณฐาน
  5. การห่อหุ้มและนามธรรม
  6. เมธอดพิเศษ (__str__, __repr__ และอื่น ๆ)
← กลับไปที่ Python Academy