Most popular

วันอังคารที่ 26 กรกฎาคม พ.ศ. 2554

C# Generic กำหนดเงื่อนไขให้รับ Type ที่ Implement มาจาก Interface

Generic Type นอกจาก Type ทั่วๆไปอย่าง string , int , double และอื่นๆที่เป็นของ C# แล้ว เรายังสามารถกำหนด Type ให้เป็น Class ที่ Implement มาจาก Interface ที่ระบุใน Generic ด้วย

ภาพรวมของคำสั่งทั้งหมด จะประกอบด้วยคลาส Generic และ Interface และ Class ที่ Implement มาจาก Interface

ภายใน Interface IPerson เป็นเพียงการประกาศ method printTpListBox เพื่อให้ Class อื่นๆนำไป Implement ต่อ


    interface IPerson
    {
        void printToListBox(ListBox listbox);
    }


Class ที่นำ IPerson ไป Implement ต่อคือ Class Person ที่การทำงานคือการเก็บค่าของบุคคล โดยมี Property Fname (string) , Lname (string) , Age (int) และ Position (string) และได้ Implement method printToListBox จาก IPerson


    public class Person : IPerson
    {
        string fname;


        public string Fname
        {
            get { return fname; }
            set { fname = value; }
        }
        string lName;


        public string LName
        {
            get { return lName; }
            set { lName = value; }
        }
        int age;


        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        string position;


        public string Position
        {
            get { return position; }
            set { position = value; }
        }


        //Constructor ที่ผ่านค่า Parameter มาให้ Property
        public Person(string firstName, string lastName, int age, string position)
        {
            this.fname = firstName;
            this.lName = lastName;
            this.age = age;
            this.position = position;
        }


        //แสดงข้อมูลบุคคลใน ListBox
        public void printToListBox(ListBox listbox)
        {
            listbox.Items.Add(this.fname + "  " + this.lName + " อายุ " + this.age + " ตำแหน่ง " + this.position);
        }
    }


ต่อมาคือ Class Generic ที่กำหนดเงื่อนไขให้รับ Type เป็น Instance ใดๆที่มาจาก Class ที่ Implement มาจาก IPerson โดยที่กำหนด Key word "where" ต่อท้าย Class


    class personCollection<T> where T : IPerson
    {
        //สร้าง Collection Queue ที่ทำงานแบบ FIFO
        Queue<T> personCol = new Queue<T>();


        //method เพิ่มข้อมูล
        public void Add(T item)
        {
            personCol.Enqueue(item);
        }

        //แสดงข้อมูลผ่าน ListBox
        public void print(ListBox listbox)
        {
            while (personCol.Count > 0)
            {
                T ps = personCol.Dequeue();
                ps.printToListBox(listbox);
            }
        }
    }


การเรียกใช้งานก็เหมือนกับการใช้ Generic Type แต่เปลี่ยนจากการกำหนดชนิดข้อมูลเดิมเป็น Class ที่ Implement มาจาก Interface


    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();


            //สร้าง Instance จาก Class Person
            Person p1 = new Person("Keattipoom", "Yamyim", 23, "Sale Manager");
            Person p2 = new Person("Keatkamon", "Kunsopit", 29, "Senior Programmer");


            //สร้าง Generic plist ขึ้นใช้งาน โดยกำหนด Type เป็น Person
            personCollection<Person> plist = new personCollection<Person>();
            //เพิ่ม Instance ของ Class Person ที่มีข้อมูลอยู่แล้ว
            plist.Add(p1);
            plist.Add(p2);
            //ให้ Generic plist แสดงค่าที่ได้รับออกมาทาง ListBox
            plist.print(this.listBox1);
        }
    }


เมื่อรันโปรแกรม ข้อมูลจะถูกแสดงใน ListBox

ไม่มีความคิดเห็น:

แสดงความคิดเห็น