当前位置: 移动技术网 > IT编程>开发语言>.net > 泛型学习笔记

泛型学习笔记

2019年02月20日  | 移动技术网IT编程  | 我要评论

蔷薇格斗2,naacee,录制游戏视频软件

泛型的好处:类型安全性能高,代码重用扩展好。

泛型的使用:如果我们需要使用多个泛型来实例化一个类型,那么我们就需要使用说明性的名称,比如tid,tfirstname之类的。

泛型的约束

  where t : struct -类型t必须是值类型

  where t : class -类型t必须是引用类型

  where t : ifoo -类型t必须执行接口ifoo

  where t : foo -类型t必须继承自 foo

  where t : new() -类型t必须有一个默认构造函数

  where t : u -指定泛型类型t1派生于t2。即多个泛型类型之间有继承关系

泛型继承:类可以继承自泛型基类,泛型类也可以继承自泛型基类。有一个限制,在继承的时候,必须显示指定基类的泛型类型,型如:public class child<t>:base<int>{…}

泛型接口:需要注意的是,在实现接口时需要指定接口的泛型类型。

泛型方法:跟泛型类差不多,方法在定义的时候使用泛型类型定义参数。调用的时候使用实际类型替换。

泛型委托:如果我们需要定义的只是一个功能,但是功能的实现要到具体的地方才能确定,我们就可以使用委托,但是使用委托我们的方法返回值和参数类型就确定了,我们可以让委托具有更高等级的抽象,返回值,参数类型都到具体的地方制定。这里的具体地方就是我们要实现的方法。这样,我们的委托就具有更高级别的抽象。我们设计的类就具有更高级别的可以用性,我们只需要实现方法的细节就可以了。方法的细节怎么实现,可以使用匿名方法,或者lamda表达式。

 

这里举一个自动算工资的例子来讲一下泛型委托。先定义一个员工类salaryperson,里面包含了一个薪资累加的方法:

 1 public class salaryperson
 2 {
 3         private int _id;
 4 
 5         public int id
 6         {
 7             get { return _id; }
 8             set { _id = value; }
 9         }
10 
11         private string _name;
12  
13         public string name
14         {
15             get { return _name; }
16             set { _name = value; }
17         }
18 
19         private decimal _salary;
20  
21         public decimal salary
22         {
23             get { return _salary; }
24             set { _salary = value; }
25         }
26 
27         public salaryperson() { }
28 
29         public salaryperson(int id, string name, decimal salry)
30         {
31             this._id = id;
32             this._name = name;
33             this._salary = salry;
34         }
35 
36         /// <summary>
37         /// 薪资累加
38         /// </summary>
39         /// <param name="p"></param>
40         /// <param name="d"></param>
41         /// <returns></returns>
42         public static decimal addsalary(salaryperson p, decimal d)
43         {
44             d += p.salary;
45             return d;
46         }
47 }
view code

定义泛型委托类genericdelegate:

 1 public class genericdelegate
 2  {
 3         //定义泛型委托
 4         public delegate tresult action<tinput, tresult>(tinput input, tresult result);
 5 
 6         public static tresult getsalary<tinput, tresult>(ienumerable<tinput> e, action<tinput, tresult> action)
 7         {
 8             tresult result = default(tresult);
 9 
10             foreach (tinput t in e)
11             {
12                 result = action(t, result);
13             }
14 
15             return result;
16         }
17 }

调用代码:

 1             list<salaryperson> list = new list<salaryperson>();
 2             list.add(new salaryperson(1, "edrick", 5000));
 3             list.add(new salaryperson(1, "meci", 3000));
 4             list.add(new salaryperson(1, "jun", 1000));
 5 
 6             //genericanddelegate.action<salaryperson, decimal> a = new genericanddelegate.action<salaryperson, decimal>((m, n) => n += m.salary);
 7             genericdelegate.action<salaryperson, decimal> a = delegate (salaryperson sp, decimal s) { return s += sp.salary; };
 8             //等同于 list.foreach(m => d += m.salary);
 9             decimal d = genericdelegate.getsalary<salaryperson, decimal>(list, a);
10             console.writeline(d);
11             console.read();

 

参考资料:.net中的泛型全面解析

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网