Polymorphism
(การพ้องรูป ) คือ
คุณสมบัติที่ทำให้การทำงานของออบเจ็กต์หนึ่งมีได้หลายรูปแบบ
(เรียกว่าหนึ่งรูปหลายพฤติกรรม) เช่น คนก็มีพฤติกรรม
การวิ่ง สุนัข ก็มี พฤติกรรม การวิ่ง แต่การวิ่งของคนและสุนัขต่างกัน
กล่าวคือ คนใช้ขา 2 ขาในการวิ่ง แต่สุนัขใช้ 4 ขา ในทางของการเขียนโปรแกรมเชิงวัตถุสามารถแบ่ง
Polymorphism ได้ 2 ลักษณะคือ
Overloading
Overriding
-
คนวิ่ง
run()
-
สุนัขวิ่ง
run()

รากฐานของการพ้องรูปคือคุณสมบัติการถ่ายทอด
คุณสมบัติการถ่ายทอดยืนยันได้ว่าคลาสลูกที่เกิดจากคลาสแม่เดียวกันย่อมมีคุณสมบัติเหมือนกัน

คลาสแม่คือ
Shape
คลาสลูกคือ Circle, Triangle, Rectangle มีคุณสมบัติเหมือน
คลาสแม่ทุกประการ
เป็นที่มาของหนึ่งรูปหลายพฤติกรรม
Polymorphism ::Overloading
Overloading
คือ คุณสมบัติที่ยอมให้เราสร้างเมธอดชื่อเดียวกันตั้งแต่
2 เมธอดขึ้นไปไว้ในคลาสเดียวกันได้โดย เมธอดเหล่านั้นต้องมีจำนวนพารามิเตอร์แตกต่างกัน
หรือ มีจำนวนเท่ากันแต่ ต่างชนิดกัน ได้
ในภาษา VB.NET จะใช้ keyword
Overloads
นำหน้าเมธอดที่ต้องการทำ Overloading
Polymorphism :: Overloading Example
ต.ย. สมมุติ
เราต้องการสร้าง เมธอดสำหรับหาผลบวกของเลขสองจำนวนแต่เราไม่รู้ว่าตัวเลขที่รับเข้ามานั้น
เป็น ชนิด integer หรือ String กันแน่เราจึงจำเป็นต้องสร้าง
เมธอดเพื่อรองรับทั้งสองรูปแบบก็สามารถทำได้ดังนี้
Class Summation
Overloads
Public Function ADD(a as int16 , b as int16) as int32
Return
a+b
End Function
Overloads
Public Function ADD(a as String , b as String) as
int32
Return
Val(a) + Val(b) ' ใช้ฟังก์ชัน Val
เพื่อตัดข้อความที่ไม่จำเป็นออกที่อาจจะมีได้ เช่น Val(2aaa)
จะได้ผลลัพธ์เป็น 2
End Function
็
End Class
ทดสอบการทำงานโดย
ออกแบบหน้าจอ และเขียนโปรแกรมดังนี้

Private Sub BtnAdd_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles BtnAdd.Click
Dim sum As New summation
LbResult.Text
= sum.ADD(TextBox1.Text, TextBox2.Text)
End Sub
Polymorphism :: Overriding Example
Overrinding
คือ คุณสมบัติที่ยอมให้เราสร้างเมธอดของคลาสลูก
(Child Class ) ชื่อเดียวกันตั้งแต่ 2 เมธอดขึ้นไปไว้ในคลาสเดียวกันได้โดย
เมธอดเหล่านั้นต้องมีจำนวนพารามิเตอร์แตกต่างกัน หรือ
มีจำนวนเท่ากันแต่ ต่างชนิดกัน ได้
ในภาษา VB.NET จะใช้ keyword
Overloads
นำหน้าเมธอดที่ต้องการทำ Overloading
ต.ย. ที่ 1
Class Employee
Overridable
Function PayEmployee () As Decimal
PayEmployee
= Hours * HourlyRate
End
Function
End Class
Class CommissionedEmployee
Inherits
Employee
Overrides
Function PayEmployee ( ) As Decimal
PayEmployee
= BasePay + Commissions
End
Function
End Class
ต.ย.
ที่ 2
The
following example defines two classes. The first class
is a base class that has two methods. The second class
inherits both methods from the base class, overrides
the second method, and defines a field named Field.
Class Class1
Sub
Method1()
MessageBox.Show("This
is a method in the base class.")
End
Sub
Overridable
Sub Method2()
MessageBox.Show("This
is another method in the base class.")
End
Sub
End Class
Class
Class2
Inherits
Class1
Public
Field2 as Integer
Overrides
Sub Method2()
Messagebox.Show("This
is a method in a derived class.")
End
Sub
End Class
Protected
Sub TestOverloading()
Dim
C1 As New class1()
Dim
C2 As New class2()
C1.Method1()
' Calls a method in the base class.
C1.Method2()
' Calls another method from the base class.
C2.Method1()
' Calls an inherited method from the base class.
C2.Method2()
' Calls a method from the derived class.
End Sub