Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีทำให้โค้ดใช้ซ้ำได้ใน C#

ใช้ Interfaces เพื่อทำให้โค้ด Reusable Interface กำหนด Properties, Methods, Events แค่ Declaration Derived Class จะ Implement รายละเอียด ช่วยให้โครงสร้างมาตรฐาน

ตัวอย่าง Interface:

public interface IShape {
    void display();
}

Convention: ชื่อ Interface เริ่มต้นด้วย "I" ไม่ต้องระบุ Access Modifier เพราะเป็น public โดยค่าเริ่มต้น

ในทางปฏิบัติ Polymorphism เป็นกุญแจสำคัญของ Code Reuse Interfaces ยืดหยุ่นเพราะส่งอะไรก็ได้ที่ Implement Interface นั้น