unity3d研究之C# 泛型

泛型是什么?

这是摘自百度百科中对泛型的介绍:

?
泛型是c#2.0的一个新增加的特性,它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。通过知道使用泛型定义的变量的类型限制,编译器可以在一个高得多的程度上验证类型假设,所以泛型提高了程序的类型安全。它允许程序员将一个实际的数据类型的规约延迟至泛型的实例被创建时才确定。泛型为开发者提供了一种高性能的编程方式,能够提高代码的重用性,并允许开发者编写非常优雅的解决方案。

看过之后,会用的还是懂;不会用的,也还是不懂。

一:泛型方法

所以我们还是看看泛型具体在程序中的应用:

复制代码
//泛型方法
//unity3D中用的最多的泛型方法应该是GetCompent<T>
//T就是占位符,当调用这个方法的你要告诉编译器,这个T的具体类型。
//另外,"T"只是一个标识,完全可以用其他代替,如"K","MyType"等,只是使用习惯。
//比如:
GameObject player;
Animator animator;
void Start()
    {
//在场景中找到名为MyPlayer物体
player = GameObject.Find("MyPlayer");
//获取该物体上的Animator组件
animator = player.GetComponent<Animator>();
//对于使用AddComponent<T>()、GetCompent<T>()这两个泛型方法来说,需要了解的就是:T就是你想要的具体的组件类型。
//对于泛型方法来说,泛型的作用就是占位和约束的作用。
    }       
//下面来说声明泛型函数
    /// <summary>
    /// 比较等级;
    /// </summary>
    /// <returns>
    /// 若t1>=t2的等级,则返回true;否则返回false
    /// </returns>
    /// where T : IRole where K : IRole的作用是约束传入的两个参数类型必须要实现IRole这个接口;
    /// 这样就定义好了一个泛型方法
    public bool CompareLevel<T,K>(T t1,    K t2) where T : IRole where K : IRole
    {
        //因为泛型t1,t2都被约束需要实现接口,所以我们可以强制转换到IRole来获取level比较
        return ((IRole)t1).level >= ((IRole)t2).level;
    }
    //那么怎么使用呢?
    //接下来看:
    public void Test()
    {
        //先定义三个测试用的类型
        MyNPC npc =new MyNPC();
        MyPlayer player =new MyPlayer();
        MyMonster monster =new MyMonster();
        //对各个类型的level赋值
        npc.level =1;
        player.level =2;
        monster.level =3;
        //比较npc和player的level就很简单了,只需要这样调用即可
        bool b1 = CompareLevel<MyNPC,MyPlayer>(npc,player); //npc?payer//false                    
        bool b2 = CompareLevel<MyNPC,MyMonster>(npc,monster);//npc?monster//false
        bool b3 = CompareLevel<MyPlayer,MyMonster>(player,monster);//payer?monster//false
    }
    
    public interface IRole 
    {
        int level{get;set;}
    }
    public class MyPlayer:IRole
    {
        public int level{get;set;}
    }
    public class MyNPC:IRole
    {
        public int level{get;set;}
    }
    public class MyMonster:IRole
    {
        public int level{get;set;}
    }
复制代码

二:泛型类

最典型的例子就是List:

List<T>类是  ArrayList 类的泛型等效类。 该类使用大小可按需动态增加的数组实现  IList<T> 泛型接口。

List<T> mList = new List<T>();   

示例代码如下:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         //从A产线出来的生产日期是string类型的"20130610",水是矿泉水,温度是20,  
  7.         Water<string, WaterBase> test1 = new Water<string, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于string类型  
  8.         test1.data = "20130610";  
  9.         test1.info.name = "KuangQuanShui";  
  10.         test1.info.temperature = 20;  
  11.         //从B产线出来的生产日期是int类型的20130610,水是纯净水,温度是20,  
  12.         Water<int, WaterBase> test2 = new Water<int, WaterBase>();//Water<任意类型,直接收WaterBase类型> 在此"T"相当于int类型  
  13.         test2.data = 20130610;  
  14.         test2.info.name = "ChunJingShui";  
  15.         test2.info.temperature = 20;  
  16.     }  
  17. }  
  18. public class Water<T, U> where U : WaterBase //限定"U"只能接收WaterBase类型  
  19. {  
  20.     public T data;//出厂日期(可接受int型的20130610,或者string类型的"20130610");  
  21.     public U info;//水的具体信息(矿泉水/纯净水...温度)  
  22. }  
  23. public class WaterBase  
  24. {  
  25.     public string name;  
  26.     public int temperature;  
  27. }  
复制代码

接下来是泛型的继承:

  1. public class fanxing : MonoBehaviour  
  2. {  
  3.     // Use this for initialization  
  4.     void Start()  
  5.     {  
  6.         TestChild1 test = new TestChild1();  
  7.         test.data = "KuangQuanShui";  
  8.         Testchild2 test2 = new Testchild2();  
  9.         test2.data = 100;  
  10.   
  11.         Son1 son1 = new Son1();  
  12.         son1.data1 = "KuangQuanShui";  
  13.         son1.data2 = 100;  
  14.     }  
  15. }  
  16. #region 一个占位符  
  17. public class Test<T>  
  18. {  
  19.     public T data;  
  20. }  
  21. public class TestChild1 : Test<string> { }  
  22. //或者: public class TestChild1<T>: Test<string> { }  
  23. //或者: public class TestChild1<T>: Test<T> { }  
  24. public class Testchild2 : Test<int> { }  
  25. #endregion  
  26. #region 两个占位符  
  27. public class Fater<T, U>  
  28. {  
  29.     public T data1;  
  30.     public U data2;  
  31. }  
  32. public class Son1 : Fater<string, int> { }  
  33. //或者: public class Son1<T,U> : Fater<string, int> { }  
  34. //或者: public class Son1<T,U> : Fater<T, U> { }  
复制代码

三:泛型接口



  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 泛型
  6. {
  7.     class 泛型接口
  8.     {
  9.         public static void Main()
  10.         {
  11.             PersonManager man = new PersonManager();
  12.             Person per = new Person();
  13.             man.PrintYourName(per);
  14.             Person p1 = new Person();
  15.             p1.Name = "p1";
  16.             Person p2 = new Person();
  17.             p2.Name = "p2";
  18.             man.SwapPerson<Person>(ref p1, ref p2);
  19.             Console.WriteLine( "P1 is {0} , P2 is {1}" , p1.Name ,p2.Name);
  20.             Console.ReadLine();
  21.         }
  22.     }
  23.     //泛型接口
  24.     interface IPerson<T>
  25.     {
  26.         void PrintYourName( T t);
  27.     }
  28.     class Person
  29.     {
  30.         public string Name = "aladdin";
  31.     }
  32.     class PersonManager : IPerson<Person>
  33.     {
  34.         #region IPerson<Person> 成员
  35.         public void PrintYourName( Person t )
  36.         {
  37.             Console.WriteLine( "My Name Is {0}!" , t.Name );
  38.         }
  39.         #endregion
  40.         //交换两个人,哈哈。。这世道。。
  41.         //泛型方法T类型作用于参数和方法体内
  42.         public void SwapPerson<T>( ref  T p1 , ref T p2)
  43.         {
  44.             T temp = default(T) ;
  45.             temp = p1;
  46.             p1 = p2;
  47.             p2 = temp;
  48.         }
  49.     }
  50. }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值