泛型五大约束



//泛型类型约束//
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
namespace  泛型的类型约束
{
     class  Program
    {
         static  void  Main( string [] args)
        {
             //int a = 3, b = 20;
             //A<int> ah = new A<int>();
             //Console.WriteLine(ah.P(a, b));
             //int a = 10;
             C  c =  new  C ();
             B < C > bh =  new  B < C >();
            bh.P(c);
             //Console.WriteLine(b);
        }
    }
     public  interface  IM
    {
         int  Change();
         string  Compare();
    }
     //public class A<T>where T :IComparable
     //{
     //    public  T P(T a, T b)
     //    {
     //        if (a.CompareTo(b)>0)
     //        {
     //            return a;
     //        }
     //        else
     //        {
     //            return b;
     //        }
     //    }
     //}
     public  class  C : IM
    {
         public    int  Change()
        {
             Console .WriteLine( "这是接口IM的Change方法" );
             return  0;
        }
         public    string  Compare()
        {
             Console .WriteLine( "这是接口IM的Compare方法" );
             return  null ;
        }
    }
     public  class  B  < T > where  T  : IM
    {        
         public  T  P( T  a)
        {
             if  (a.Change() > 0)
            {
                 return  a;
            }
             return  a;
        }
}
   
}


//泛型五大类型约束
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
namespace  泛型的五大约束
{
     泛型类的五大约束
     // 1. where T:struct  限定当前参数类型必须是值类型
     // 2. where T:class   限定当前类型参数类型必须是引用类型
     // 3. where T:new() 限定当前参数类型必须有一个无参构造器
     // 4. where T:<base class name>   限定当前参数类型 必须是当前类  或者当前类为基类的类
     //父类名
     // 5. where T:<interface name>限定当前参数类型必须实现指定接口 
     class  Program
    {
         static  void  Main( string [] args)
        {
             //D<Base> d = new D<Base>();
             //Base ba = new Base();
             //Console.WriteLine("泛型类D的  funcd的方法  在此被调用");
             //d.funcd(ba);//此处调用funcd的方法
             //C<Cb> c = new C<Cb>();
             //c.f();
             F  f =  new  F ();
             E < F > e =  new  E < F >();
            e.funce(f);
        }
    }
     public  class  Base
    {
         public  void  fb()
        {
             Console .WriteLine( "这是基类Base的方法fb" );
        }
    }
     public  class  A < T > where  T : struct
    {
         public  void  funca( T  a)
        {
        }
    }
     public  class  B
    {
    }
     public  class  C < T >  where  T  : new ()
    {
         public  void  f()
        {
             Console .WriteLine( "这里是无参构造函数的方法" );
        }
    }
     public  class  Cb
    {
         public  Cb()
        {
        }
      
    }
     public  class  D < T > where  T  : Base
    {
         public  void  funcd( T  a)
        {
            a.fb();
        }
    }
     public  interface  INter
    {
         int  funinter();
    }
     public  class  F : INter
    {
         public    int  funinter()
        {
             Console .WriteLine( "这里是实现了继承接口内的方法" );
             return  0;
        }
    }
     public  class  E < T >  where  T : INter // 第五种类型
    {
         public  T  funce( T  a)
        {
            a.funinter();
             Console .WriteLine( "这里是E内的方法的实现" );
             return  a;
         
        }
    }
   
}
//泛型类型约束//
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
namespace  泛型的类型约束
{
     class  Program
    {
         static  void  Main( string [] args)
        {
             //int a = 3, b = 20;
             //A<int> ah = new A<int>();
             //Console.WriteLine(ah.P(a, b));
             //int a = 10;
             C  c =  new  C ();
             B < C > bh =  new  B < C >();
            bh.P(c);
             //Console.WriteLine(b);
        }
    }
     public  interface  IM
    {
         int  Change();
         string  Compare();
    }
     //public class A<T>where T :IComparable
     //{
     //    public  T P(T a, T b)
     //    {
     //        if (a.CompareTo(b)>0)
     //        {
     //            return a;
     //        }
     //        else
     //        {
     //            return b;
     //        }
     //    }
     //}
     public  class  C : IM
    {
         public    int  Change()
        {
             Console .WriteLine( "这是接口IM的Change方法" );
             return  0;
        }
         public    string  Compare()
        {
             Console .WriteLine( "这是接口IM的Compare方法" );
             return  null ;
        }
    }
     public  class  B  < T > where  T  : IM
    {        
         public  T  P( T  a)
        {
             if  (a.Change() > 0)
            {
                 return  a;
            }
             return  a;
        }
}
   
}


//泛型五大类型约束
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
namespace  泛型的五大约束
{
     泛型类的五大约束
     // 1. where T:struct  限定当前参数类型必须是值类型
     // 2. where T:class   限定当前类型参数类型必须是引用类型
     // 3. where T:new() 限定当前参数类型必须有一个无参构造器
     // 4. where T:<base class name>   限定当前参数类型 必须是当前类  或者当前类为基类的类
     //父类名
     // 5. where T:<interface name>限定当前参数类型必须实现指定接口 
     class  Program
    {
         static  void  Main( string [] args)
        {
             //D<Base> d = new D<Base>();
             //Base ba = new Base();
             //Console.WriteLine("泛型类D的  funcd的方法  在此被调用");
             //d.funcd(ba);//此处调用funcd的方法
             //C<Cb> c = new C<Cb>();
             //c.f();
             F  f =  new  F ();
             E < F > e =  new  E < F >();
            e.funce(f);
        }
    }
     public  class  Base
    {
         public  void  fb()
        {
             Console .WriteLine( "这是基类Base的方法fb" );
        }
    }
     public  class  A < T > where  T : struct
    {
         public  void  funca( T  a)
        {
        }
    }
     public  class  B
    {
    }
     public  class  C < T >  where  T  : new ()
    {
         public  void  f()
        {
             Console .WriteLine( "这里是无参构造函数的方法" );
        }
    }
     public  class  Cb
    {
         public  Cb()
        {
        }
      
    }
     public  class  D < T > where  T  : Base
    {
         public  void  funcd( T  a)
        {
            a.fb();
        }
    }
     public  interface  INter
    {
         int  funinter();
    }
     public  class  F : INter
    {
         public    int  funinter()
        {
             Console .WriteLine( "这里是实现了继承接口内的方法" );
             return  0;
        }
    }
     public  class  E < T >  where  T : INter // 第五种类型
    {
         public  T  funce( T  a)
        {
            a.funinter();
             Console .WriteLine( "这里是E内的方法的实现" );
             return  a;
         
        }
    }
   
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值