1 / 7

พอร์ลีมอร์ฟิซึม Polymorphism

พอร์ลีมอร์ฟิซึม Polymorphism. ผศ.ปัญญาพล หอระตะ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยขอนแก่น. พอลีมอร์ฟิซึมคืออะไร. พอลีมอร์ฟิซึม เป็นแนวคิดที่ต้องการเขียนโปรแกรมให้ยืดหยุ่นที่สุดโดยไม่ต้องแก้ไขบ่อย หรือเมื่อมีการเพิ่มสิ่งใหม่ยังคงทำงานต่อไปได้โดยไม่กระทบกับคำสั่งเดิม

amelia
Télécharger la présentation

พอร์ลีมอร์ฟิซึม Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. พอร์ลีมอร์ฟิซึมPolymorphism ผศ.ปัญญาพล หอระตะ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยขอนแก่น

  2. พอลีมอร์ฟิซึมคืออะไร • พอลีมอร์ฟิซึม • เป็นแนวคิดที่ต้องการเขียนโปรแกรมให้ยืดหยุ่นที่สุดโดยไม่ต้องแก้ไขบ่อย หรือเมื่อมีการเพิ่มสิ่งใหม่ยังคงทำงานต่อไปได้โดยไม่กระทบกับคำสั่งเดิม • ใช้หลักการสืบทอด คลาสแม่ถือว่าเป็นส่วนที่สากล (generalization) ที่คลาสลูกสืบทอดไปได้ • คลาสแม่ถือว่าเป็นเซตของสมาชิกที่ใหญ่กว่าเซตของสมาชิกของคลาสลูก สมาชิกของคลาสลูก ก็ถือว่าเป็นสมาชิกของคลาสแม่เช่นเดียวกัน

  3. พอลีมอร์ฟิซึมคืออะไร Vehicle Tuck Car Bicycle Vehicle * v1 = new Car(); Vehicle * v2 = new Tuck(); Vehicle * v3 = new Bicycle(); Car * c= new Vehicle(); // สมาชิกของคลาสลูก ก็ถือว่าเป็นสมาชิกของคลาสแม่เช่นเดียวกัน สมาชิกของแม่ไม่ถือว่าเป็นสมาชิกของคลาสลูก

  4. ตัวอย่าง class Car: public Vehicle { private: int engineId; public: Car():Vehicle(4){}; Car(int d,int w=4): Vehicle(w){ engineId=d;} int getEngineId(){ return engineId;} }; int main() { Vehicle * v = new Car(4,123 cout << v->getWheel() << endl; cout << v->getEngineId() <<endl; //error return 0; } #include <iostream> using namespace std; class Vehicle { public: Vehicle(){ } Vehicle(int w){ wheel=w;} int getWheel(){ return wheel;} private: int wheel; };

  5. Virtual Functions • Virtual function • เป็นการระบุให้มีการกำหนดรุ่นของfunction ที่มีชื่อซ้ำกันระหว่างคลาสแม่กับคลาสลูกว่าเป็นของแม่หรือของลูก ถ้าไม่ระบุจะหมายถึงของ function ของคลาสแม่ แต่ถ้าระบุจะเลือกทำของคลาสลูกก่อน ถ้าไม่มีจึงจะไปทำของคลาสลูก • Virtual เป็นการระบุว่าให้มีการทับซ้อน (override) ฟังก์ชันนั้นได้

  6. #include <iostream> using namespace std; class Vehicle { public: Vehicle(){ } Vehicle(int w){ wheel=w;} int getWheel(){ return wheel;} void print(){ cout << “Vehicle”;} private: int wheel; }; class Car: public Vehicle { private: int engineId; public: Car():Vehicle(4){}; Car(int d,int w=4): Vehicle(w){ engineId=d;} int getEngineId(){ return engineId;} void print(){ cout << “Car”;} }; int main() { Vehicle * v = new Car(4,123 cout << v->getWheel() << endl; cout << v->getEngineId() <<endl; //error return 0; }

  7. แบบฝึกหัด • จงเขียนโปรแกรมทำ Polymorphism ของ Shape ที่มี Shape Rectangle Line Circle ให้มีการทำ override ฟังก์ชัน draw ด้วย

More Related