1: class StaffInfo<T>
   2: {
   3:     public T Num;
   4:     public T Name;
   5:     public T Sex;
   6:     public T Age;
   7:     public T Birth;
   8:     public T Salary;
   9: }

image

   1: private void button1_Click(object sender, EventArgs e)
   2:      {
   3:          StaffInfo<object> staff = new StaffInfo<object>();
   4:          staff.Num = 1;
   5:          staff.Name = "Ryan Wang";
   6:          staff.Sex = "male";
   7:          staff.Age = 18;
   8:          staff.Birth = Convert.ToDateTime("1996-01-02");
   9:          staff.Salary = 1000.43F;
  10:          //---------------------------------------
  11:          textBox1.Text = staff.Num.ToString();
  12:          textBox2.Text = staff.Name.ToString();
  13:          textBox3.Text = staff.Sex.ToString();
  14:          textBox4.Text = staff.Age.ToString();
  15:          DateTime dt = (DateTime)staff.Birth;
  16:          textBox5.Text = dt.ToShortDateString();
  17:          textBox6.Text = staff.Salary.ToString();
  18:      }

image

透過繼承泛型類別

   1: class ScoreInfo<T> : StaffInfo<T>
   2: {
   3:     public T Chinese;
   4:     public T Math;
   5:     public T English;
   6: }

宣告score變數

   1: ScoreInfo<object> score;
   1: private void button4_Click(object sender, EventArgs e)
   2:        {
   3:            score = new ScoreInfo<object>();
   4:            score.Num = 1;
   5:            score.Name = "Ryan Wang";
   6:            score.Sex = "male";
   7:            score.Age = 18;
   8:            score.Birth = Convert.ToDateTime("1996-01-02");
   9:            score.Salary = 1000.43F;
  10:            score.Chinese = 100;
  11:            score.Math = 90;
  12:            score.English = 98;
  13:            //--------------------------------------------------
  14:            textBox1.Text = score.Num.ToString();
  15:            textBox2.Text = score.Name.ToString();
  16:            textBox3.Text = score.Sex.ToString();
  17:            textBox4.Text = score.Age.ToString();
  18:            DateTime dt = (DateTime)score.Birth;
  19:            textBox5.Text = dt.ToShortDateString();
  20:            textBox6.Text = score.Salary.ToString();
  21:            textBox7.Text = score.Chinese.ToString();
  22:            textBox8.Text = score.Math.ToString();
  23:            textBox9.Text = score.English.ToString();
  24:        }

image

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

天天向上

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