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

พหุรูปคืออะไร
พหุรูปคืออะไร
พหุรูปช่วยให้เมธอดในคลาสต่าง ๆ ใช้ชื่อเดียวกันได้ แต่มีการทำงานแตกต่างกัน ทำให้โค้ดยืดหยุ่นและต่อยอดได้ง่ายขึ้น
# 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พหุรูปแบบ with เมธอด
พหุรูปแบบ with เมธอด
เมธอดที่มีชื่อเดียวกันในคลาสที่แตกต่างกันสามารถมีการนำไปใช้งานที่แตกต่างกันได้
# 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ตัวอย่างพหุรูปแบบด้วยเมธอด
พหุรูปแบบ with เมธอด
เมธอดที่มีชื่อเดียวกันในคลาสที่แตกต่างกันสามารถมีการนำไปใช้งานที่แตกต่างกันได้
# 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พหุรูปแบบ with ฟังก์ชัน
พหุรูปแบบ with ฟังก์ชัน
พหุรูปแบบทำให้ฟังก์ชันเดียวกันสามารถทำงานกับออบเจ็กต์ต่างชนิดกันได้
# 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พหุรูปแบบ with การสืบทอด
พหุรูปแบบ with การสืบทอด
พหุรูปแบบมักเกิดขึ้นผ่านการสืบทอด โดยคลาสย่อยจะแทนที่เมธอดจากคลาสแม่
# 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) ในการสืบทอดหลายรูปแบบ
- ใช้พหุรูปแบบโดยไม่จำเป็นกับงานง่าย ๆ
เราได้เรียนรู้อะไรบ้าง
เราได้เรียนรู้อะไรบ้าง
ในบทเรียนนี้ คุณได้เรียนรู้:
- พหุรูปแบบคืออะไรและทำงานอย่างไรใน Python
- วิธีนำพหุรูปแบบไปใช้ with เมธอดและการสืบทอด
- ข้อดีของการใช้พหุรูปแบบใน OOP
ทำได้ดีมาก! ไปยังหัวข้อถัดไปกันเลย

คำถามที่พบบ่อย
บทเรียน “พหุสัณฐาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พหุสัณฐาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Learn AI with Python ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Learn AI with Python มีบทเรียนทั้งหมด 6 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พหุสัณฐาน”
สำรวจว่าพหุสัณฐานช่วยให้โค้ดยืดหยุ่นและทำงานแบบไดนามิกได้อย่างไร คุณปฏิบัติ Learn AI with Python ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Learn AI with Python หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Learn AI with Python บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 6 บทเรียน
บทเรียน “พหุสัณฐาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Learn AI with Python นี้ได้ไหม
ได้ บทเรียน Learn AI with Python ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คลาสและออบเจ็กต์
- แอตทริบิวต์และเมธอด
- การสืบทอด
- พหุสัณฐาน
- การห่อหุ้มและนามธรรม
- เมธอดพิเศษ (__str__, __repr__ และอื่น ๆ)