close

選擇類別庫,  名稱: MyToolkit

image

點選MyToolkit方案按下滑鼠右鍵 <加入>< 新增項目>

image

選擇: <類別>, 輸入GenericTool.cs

image

加入保留字<public>, 以便其他專案加入參考後看得到

 

image

 

   1: namespace MyToolkit
   2: {
   3:     public class GenericTool
   4:     {
   5:         public static int Finder<T>(T[] items, T item)
   6:         {
   7:             for (int i = 0; i < items.Length; i++)
   8:             {
   9:                 if(items[i].Equals(item))
  10:                 {
  11:                     return i;
  12:                 }
  13:             }
  14:             return -1;
  15:         }
  16:     }
  17: }

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

加入另一個新的專案, 測試剛才寫好函式庫中的 Finder泛型函式

針對int型別

   1: int ind;
   2: int[] a = new int[8] { 1, 23, 24, 5, 2, 4, 33, 4 };
   3: int b = 5;
   4: ind = GenericTool.Finder<int>(a, b);
   5: MessageBox.Show( b.ToString() + "在a[]中 第 " + ind.ToString() + " 位置" );

執行結果:

image

針對char型別

   1: char[] c = new Char[5] { 'a', 'b', 'c', 'd', 'e' };
   2: char d = 'e';
   3: ind  = GenericTool.Finder<char>(c, d);
   4: MessageBox.Show(d + "在c[]中 第 " + ind.ToString() + " 位置");
針對string型別
   1: string[] f = new string[3] { "你", "我", "他" };    
   2: string  g = "我";
   3: ind = GenericTool.Finder<string>(f, g);
   4: MessageBox.Show(d + "在f[]中 第 " + ind.ToString() + " 位置");
針對DateTime型別
   1: DateTime[] h = new DateTime[3] { new DateTime(2015, 2, 14), new DateTime(2015, 2, 15), new DateTime(2015, 2, 16) };
   2: DateTime i = new DateTime(2015,2,15);
   3: ind = GenericTool.Finder<DateTime>(h, i);
   4: MessageBox.Show(i.ToShortDateString() + "在h[]中 第 " + ind.ToString() + " 位置");

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class [類別名稱]<類型參數T>

{

public T 欄位名稱1

public T 欄位名稱2

…    …

}

開啟GenericTool.cs,  加入一個泛型類別 Employee

   1: #region 員工基本資料類別
   2: public class Employee<T>
   3: {
   4:     public T num;
   5:     public T name;
   6:     public T sex;
   7:     public T age;
   8:     public T birthday;
   9:     public T salary;
  10: }
  11: #endregion

完成如下

image

 

更新編譯函式庫MyToolkit

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

撰寫一個範例測試, 並加入參考MyToolkit

以Employee.num為例, 等號右邊型別為int, 因此原本employ.num為object型態可以動態更新成int型態

同理, birthday等號右邊為DateTime, 所以employee.birthday就會變成DateTime型態

心得:定義Employ為泛型類別, 這樣有個好處是時不用擔心到時候實體化之後傳入內容資料型態,

是否與當初設計的資料型態相吻合, 有可能設計時num欄位時想的是int就夠用, 結果使用時該死傳入double就掛點啦!!!

泛型可以在等號右邊傳入時候, 才決定其真正的資料型態

   1: Employee<object> employee = new Employee<object>();  //實例化泛型類物件
   2: employee.num = 1;
   3: employee.name = "小王";
   4: employee.sex = "渣啵";
   5: employee.age = 25;
   6: employee.birthday = Convert.ToDateTime("1995/11/02");
   7: employee.salary = 1000F;
   8: //-------------------------------------
   9: textBox1.Text = employee.num.ToString();
  10: textBox2.Text = employee.name.ToString();
  11: textBox3.Text = employee.sex.ToString();
  12: textBox4.Text = employee.age.ToString();
  13: textBox5.Text = employee.birthday.ToString();
  14: textBox6.Text = employee.salary.ToString();

image

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

繼承泛型類別

假設Employee想要增加3個欄位 住址, E-mail, 小孩個數,

直覺的想法是直接修改Employee類別,

不過這樣原本專案中有用到Employee類別也同時多了一些沒用到的欄位,

更好的最法可以新增一個類別EmployeInfo繼承Employee,

這樣新的專案就可以依照需求選擇合適的類別

   1: #region 員工基本資料和更新資訊類別
   2: public class EmployeeInfo<T> : Employee<T>
   3: {
   4:     public T address;   // 地址
   5:     public T eMail;     // 電子信箱
   6:     public T children;  // 小孩個數 
   7: }
   8: #endregion

 

更新編譯函式庫MyToolkit

image

 

------------------------------------------------------------------------------------------------------------------------------------------------------------

撰寫一個範例測試, 並加入參考MyToolkit

   1: EmployeeInfo<object> employeeInfo = new EmployeeInfo<object>();  //實例化繼承泛型類物件
   2: employeeInfo.num = 1;
   3: employeeInfo.name = "小王";
   4: employeeInfo.sex = "渣啵";
   5: employeeInfo.age = 25;
   6: employeeInfo.birthday = Convert.ToDateTime("1995/11/02");
   7: employeeInfo.salary = 1000F;
   8: employeeInfo.address = "台北市中山區南京東路二段98號";
   9: employeeInfo.eMail = "me1237guy@yahoo.com.tw";
  10: employeeInfo.children = 2;
  11: //-----------------------------------------------
  12: textBox1.Text = employeeInfo.num.ToString();
  13: textBox2.Text = employeeInfo.name.ToString();
  14: textBox3.Text = employeeInfo.sex.ToString();
  15: textBox4.Text = employeeInfo.age.ToString();
  16: textBox5.Text = employeeInfo.birthday.ToString();
  17: textBox6.Text = employeeInfo.salary.ToString();
  18: textBox7.Text = employeeInfo.address.ToString();
  19: textBox8.Text = employeeInfo.eMail.ToString();
  20: textBox9.Text = employeeInfo.children.ToString();

執行如下:

image

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()