31 泛型,partial类,枚举,结构体

在这里插入图片描述泛型:
在这里插入图片描述
正交性:泛型和其它的编程实体都有正交点,导致泛型对编程的影响广泛而深刻。
与类、接口、字段、属性的结合:

class Demo_Generic
    {
        public void test()
        {
            Demo31_Apple demo31_Apple = new Demo31_Apple() { Color = "Red" };
            Demo31_Box<Demo31_Apple> demo31_Box = new Demo31_Box<Demo31_Apple>() { Cargo = demo31_Apple};
            Console.WriteLine(demo31_Box.Cargo.Color);

            Demo31_Book demo31_Book = new Demo31_Book() { Name = "abc" };
            Demo31_Box<Demo31_Book> demo31_Box2 = new Demo31_Box<Demo31_Book>() { Cargo = demo31_Book };
            Console.WriteLine(demo31_Box2.Cargo.Name);

            Student<int> student = new Student<int>() { Name = "Li", ID = 12301201 };
            Console.WriteLine($"{student.ID}{student.Name}");

            Student2 student2 = new Student2() { Name = "Wang", ID = 12301201 };
            Console.WriteLine($"{student2.ID}{student2.Name}");
        }
    }

    class Demo31_Apple
    {
        public string Color { get; set; }
    }

    class Demo31_Box<T>
    {
        public T Cargo { get; set; }
    }

    class Demo31_Book
    {
        public string Name { get; set; }
    }

    interface IUnique<TId>
    {
        TId ID { get; set; }
    }

    class Student<TId> : IUnique<TId>
    {
        public TId ID { get; set; }
        public string Name { get; set; }
    }

    class Student2 : IUnique<int>
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }

与委托、方法的结合:

public void test()
        {
            IDictionary<int, string> dict = new Dictionary<int, string>();
            dict[1] = "a";
            dict[2] = "b";
            Console.WriteLine(dict[1]);
            Console.WriteLine(dict[2]);

            int[] a1 = { 1, 2, 3, 4, 5 };
            int[] a2 = { 1, 2, 3, 4, 5, 6 };
            double[] a3 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
            double[] a4 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
            var result = Zip(a3, a4);
            Console.WriteLine(string.Join(",", result));

            Action<string> action = Say;
            action.Invoke("123");
            Action<int> action2 = Mul;
            action2(20);

            Func<double, double,double> action3 = (a,b) => { return a+b; };//lamda与泛型的结合使用
            Console.WriteLine(action3(1,2)); 
        }

        public static T[] Zip<T>(T[] a, T[] b){
            T[] zipped = new T[a.Length + b.Length];
            int ai = 0, bi = 0, zi = 0;
            while (ai < a.Length || bi < b.Length)
            {
                if (ai < a.Length) zipped[zi++] = a[ai++];
                if (bi < b.Length) zipped[zi++] = b[bi++];
            }
            return zipped;
        } 

        private static void Say(string content)
        {
            Console.WriteLine($"Hello!{content}.");
        }

        private static void Mul(int x)
        {
            Console.WriteLine($"{x*100}");
        }

partial类:
可以分开定义同一个类
枚举、结构体:
在这里插入图片描述
枚举类示例:

class Demo_Enum
    {

        public void test()
        {
            Demo31_Person demo31_Person = new Demo31_Person();
            Console.WriteLine(demo31_Person.Level);
            demo31_Person.Level = Demo31_Level.Manager;
            Console.WriteLine((int)demo31_Person.Level);//Manager是101
            Console.WriteLine((int)Demo31_Level.Boss);//Boss是102
            Console.WriteLine(Demo31_Level.BigBoss > Demo31_Level.Boss);

            demo31_Person.Name = "Timothy";
            demo31_Person.Skill = Demo31_Skill.Cook | Demo31_Skill.Drive;
            if (((int)demo31_Person.Skill & (int)Demo31_Skill.Cook) == (int)Demo31_Skill.Cook)//可以不用强转为int
            {
                Console.WriteLine($"He can {Demo31_Skill.Cook}");
            }
        }
    }

    enum Demo31_Level{
        Employee = 100,
        Manager,
        Boss,
        BigBoss,
    }

    enum Demo31_Skill
    {
        Drive = 1,
        Cook = 2,
        Program = 4,
        Teach = 8,
    }

    class Demo31_Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public Demo31_Level Level { get; set; }
        public Demo31_Skill Skill { get; set; }
    }

结构体就没什么好展示的了
结构体类型只能由接口类型派生,结构体无法作为基类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值