Polymorphism คือ ความสามารถของวัตถุที่มาจาก class ต่าง ๆ ที่สามารถใช้ method เดียวกันได้ โดยไม่จำเป็นต้องรู้ว่ามันมาจาก class ไหน นี่เป็นหนึ่งในหลักการสำคัญของ OOP (Object-Oriented Programming) ซึ่งช่วยทำให้โค้ดมีความยืดหยุ่นและขยายตัวได้ง่ายขึ้น
ใน Python มีการใช้งาน polymorphism อยู่ในหลายรูปแบบ ได้แก่
- Polymorphism กับ Inheritance
- Polymorphism ผ่าน Method Overriding
- Polymorphism ผ่าน Function Overloading (ไม่มีโดยตรงใน Python แต่ใช้วิธีอื่นทดแทนได้)
- Polymorphism ผ่าน Operator Overloading
Polymorphism กับ Inheritance (การสืบทอดคลาส)
ในกรณีนี้ subclass (คลาสลูก) จะสืบทอด method จาก superclass (คลาสแม่) และเราสามารถเรียกใช้ method เดียวกันในคลาสลูกได้
class Animal:
def sound(self):
return "Some generic sound"
class Dog(Animal):
def sound(self):
return "Woof!"
class Cat(Animal):
def sound(self):
return "Meow!"
def make_sound(animal):
print(animal.sound())
# Polymorphism in action
dog = Dog()
cat = Cat()
make_sound(dog) # Output: Woof!
make_sound(cat) # Output: Meow!ในตัวอย่างนี้ make_sound() สามารถทำงานกับ object ของ Dog และ Cat ซึ่งเป็น subclass ของ Animal โดยไม่ต้องสนใจว่าคลาสไหนเป็นต้นทาง
Polymorphism ผ่าน Method Overriding (การเขียนทับเมธอด)
ในกรณีนี้ subclass จะเขียนทับ method เดียวกันที่มีอยู่ใน superclass โดยกำหนดพฤติกรรมเฉพาะของตนเอง
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
# Polymorphism with overridden method
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(shape.area())
# Output:
# 78.5 (พื้นที่ของวงกลม)
# 24 (พื้นที่ของสี่เหลี่ยม)ในตัวอย่างนี้ Circle และ Rectangle ต่างมี method ชื่อเดียวกันคือ area() แต่มีการคำนวณแตกต่างกันตามรูปทรงที่แตกต่างกัน
Polymorphism ผ่าน Function Overloading (การใช้ฟังก์ชันที่รับพารามิเตอร์ต่างกัน)
Python ไม่มี function overloading แบบภาษาที่เป็น strongly typed เช่น C++ หรือ Java แต่เราสามารถใช้ default parameters หรือ *args และ **kwargs เพื่อเลียนแบบลักษณะการทำงานนี้ได้
class MathOperations:
def add(self, *args):
return sum(args)
math_op = MathOperations()
print(math_op.add(2, 3)) # Output: 5
print(math_op.add(2, 3, 4, 5)) # Output: 14ในตัวอย่างนี้ method add() สามารถทำงานได้ทั้งการบวก 2 จำนวนหรือมากกว่านั้น โดยการใช้ *args
Polymorphism ผ่าน Operator Overloading (การใช้งาน polymorphism กับการ overload operators)
Python อนุญาตให้เราสามารถเปลี่ยนพฤติกรรมของ operators (เช่น +, -, *, /) โดยใช้ method พิเศษที่ขึ้นต้นด้วย __ และลงท้ายด้วย __
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2 # ใช้ __add__ method
print(v3) # Output: Vector(6, 8)ในตัวอย่างนี้ operator + ถูก overload โดยการใช้ method __add__() เพื่อให้สามารถบวกเวกเตอร์ 2 ตัวได้
สรุป
Polymorphism ใน Python สามารถใช้งานได้ในหลายรูปแบบดังนี้
- การใช้กับ inheritance ทำให้ subclass มี method และพฤติกรรมเฉพาะของตัวเอง
- การ override method เพื่อกำหนดพฤติกรรมใหม่ใน subclass
- การใช้
*argsและ**kwargsเพื่อทำงานกับฟังก์ชันที่ต้องการการใช้งานแบบ overloading - การ operator overloading เพื่อเปลี่ยนพฤติกรรมของ operators
Polymorphism ช่วยให้โค้ดมีความยืดหยุ่นมากขึ้น ลดการเขียนโค้ดซ้ำ และสามารถปรับใช้กับ class หรือ object ใหม่ได้ง่าย
