泛型五大约束

来源:互联网 发布:查看linux下的用户 编辑:程序博客网 时间:2024/05/21 04:23


//////////////////////////////////////////////////泛型类型约束//////////////////////////////////
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 0
原创粉丝点击