Unity3d-C#之where

原文:http://blog.csdn.net/cabxyz/article/details/41864577

where简单的说就是做范型约束,更多内容参见官方文档:http://msdn.microsoft.com/zh-cn/library/bb384067.aspx

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. namespace TestCSharp  
  2. {  
  3.     class BaseView  
  4.     {  
  5.         public int ViewId;  
  6.         public string ViewName;  
  7.   
  8.         public string GetInfo()  
  9.         {  
  10.             return string.Format("viewid:{0},viewName:{1}",this.ViewId,this.ViewName);  
  11.         }  
  12.     }  
  13. }  

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. namespace TestCSharp  
  2. {  
  3.     class BattleView : BaseView  
  4.     {  
  5.         public string CityName;  
  6.         public int Hp;  
  7.   
  8.         public BattleView()  
  9.         {  
  10.             this.ViewId = 1;  
  11.             this.ViewName = "BattleView";  
  12.         }  
  13.     }  
  14. }  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. namespace TestCSharp  
  2. {  
  3.     class LoginView : BaseView  
  4.     {  
  5.         public string UserName;  
  6.         public string Password;  
  7.   
  8.         public LoginView()  
  9.         {  
  10.             this.ViewId = 0;  
  11.             this.ViewName = "LoginView";  
  12.         }  
  13.     }  
  14. }  

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2.   
  3. namespace TestCSharp  
  4. {  
  5.     class ViewFactory<T> where T : BaseView, new()  
  6.     {  
  7.         private readonly T mView = new T();  
  8.   
  9.         public void Print()  
  10.         {  
  11.             Console.WriteLine(mView.GetInfo());  
  12.         }  
  13.     }  
  14. }  

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace TestCSharp  
  6. {  
  7.    
  8.     class MainClass  
  9.     {  
  10.         public static void Main(string[] args)  
  11.         {  
  12.             ViewFactory<LoginView> loginView = new ViewFactory<LoginView>();  
  13.             loginView.Print();  
  14.   
  15.             ViewFactory<BattleView> battleView = new ViewFactory<BattleView>();  
  16.             battleView.Print();  
  17.   
  18.             //error  
  19.             //ViewFactory<Object> createCharView = new ViewFactory<Object>();  
  20.         }  
  21.   
  22.         
  23.     }  
  24.      
  25. }  

上面是约束构造函数,使用 new 运算符创建类型参数的实例。下面我们改造为函数

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2.   
  3. namespace TestCSharp  
  4. {  
  5.     class ViewFactory  
  6.     {  
  7.   
  8.   
  9.         public static void RegView<T>(int viewId, string viewName) where T : BaseView, new()  
  10.         {  
  11.               
  12.             T mView = new T {ViewId = viewId, ViewName = viewName};  
  13.             Console.WriteLine(mView.GetInfo());  
  14.         }  
  15.   
  16.     }  
  17. }  

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace TestCSharp  
  6. {  
  7.    
  8.     class MainClass  
  9.     {  
  10.         public static void Main(string[] args)  
  11.         {  
  12.             ViewFactory.RegView<LoginView>(100,"LoginView");  
  13.             ViewFactory.RegView<BattleView>(200, "BattleView");  
  14.         }  
  15.   
  16.         
  17.     }  
  18.      
  19. }  

同样也是创建对象的约束。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值