泛型

泛型方法:

 public static void sd<T>(T t)
        {
            Console.WriteLine("名称{0},类型{1}",t,t.GetType());
        }

调用泛型方法:

 qiu.sd<string>("hello");

 

泛型方法加约束:

 public static T hi<T>(T t) where T : people, sport, work, new()
        {
            T model = new T();
            model.id = 11;
            model.name = "wang";
            model.sex = "";

            

            return model;

        }

类与接口:

 public interface sport
    {
        void run();
    }

    public interface work
    {
        void dojob();
    }

 public class people
    {
        public int id { get; set; }
        public string name { get; set; }

        public string sex { get; set; }

    }

Chinese类:继承接口后必须实现接口的所有方法

 public class chinese : people, sport, work
    {

        public void dojob()
        {
            Console.WriteLine("i can do job well");
        }

        public void run()
        {
            Console.WriteLine("i can run fast");
        }
    }

调用方法;chinese类必须满足泛型约束的条件,继承相应的类和接口

chinese rt= qiu.hi<chinese>(qc);

            Console.WriteLine("{0},{1},{2}", rt.id,rt.name,rt.sex);
            rt.run();
            rt.dojob();

 

posted @ 2017-09-08 12:00  游称  阅读(92)  评论(0编辑  收藏  举报