C#学习(16)------泛型、Partial类、枚举、结构体

一、泛型

泛型跟其他的东西具有良好的正交性。

 即存在泛型类、泛型接口、泛型委托等等等。

泛型对于成员膨胀和类型膨胀具有良好的作用。下面我们来看看实例。

本次实例讲的是一个杂货铺,一开始只卖苹果:

namespace ConsoleAppPractice
{
    class Program
    {
        static void Main()
        {
            Store store = new Store();
            Box box = new Box();
            Apple apple = new Apple(AppleColor.Red);
            store.Wrap(box, apple);
            store.Sold(box);
        }
    }
    enum AppleColor
    {
        Red,
        Green,
        Yellow,
    }
    class Store
    {
        public void Wrap(Box box,Apple apple)
        {
            Console.WriteLine("I will wrap the apple.");
            box.apple = apple;
        }
        public void Sold(Box box)
        {
            Console.WriteLine("I will sold you {0} {1} in the box.",box.apple.Color,box.apple.GetType().Name);
        }
    }
    class Box
    {
        public Apple apple { get; set; }
    }
    class Apple
    {
        public AppleColor Color { get; set; }
        public Apple(AppleColor color)
        {
            Color = color;
        }
    }
}

然后还想卖书,这时候有一种做法,是再声明一个盒子只装书。这显然会随着商品种类增多造成类型膨胀。或者是在盒子类里再多一个书的属性,这显然会造成成员膨胀。而如果使用接口,又感觉划不来。这时候,我们可以考虑使用泛型。

namespace ConsoleAppPractice
{
    class Program
    {
        static void Main()
        {
            Store store = new Store();
            Thing<AppleColor> apple = new Thing<AppleColor>(AppleColor.Red, "Apple");
            Thing<BookName> book = new Thing<BookName>(BookName.Snow_Whites, "Book");
            Box<Thing<AppleColor>> appleBox = new Box<Thing<AppleColor>>();
            Box<Thing<BookName>> bookBox = new Box<Thing<BookName>>();
            store.Wrap(appleBox, apple);
            store.Sold(appleBox);
            store.Wrap(bookBox, book);
            store.Sold(bookBox);
        }
    }
    enum AppleColor
    {
        Red,
        Green,
        Yellow,
    }
    enum BookName
    {
        Snow_Whites,
        The_Little_Prince,
        Go_West,
    }
    class Store
    {
        public void Wrap<T>(Box<T> box,T thing)
        {
            box.thing = thing;
        }
        public void Sold<T>(Box<T> box)
        {
            Console.WriteLine("I will sold you {0} in the box.",box.thing.GetType().GetProperty("thingName").GetValue(box.thing));
        }
    }
    class Box<T>
    {
        public T thing { get; set; }
    }
    class Thing<T>
    {
        public T Property { get; set; }
        public string thingName { get; set; }
        public Thing(T t,string name)
        {
            Property = t;
            thingName = name;
        }
    }
}

我能想到的最好方法。

注意一下泛型方法、泛型类的定义

注意main函数里调用方法那几行,泛型可以自动推测类型。

接下来我们看看泛型接口的实例。

namespace ConsoleAppPractice
{
    class Program
    {
        static void Main()
        {
            Student<ulong> stu1 = new Student<ulong>() { ID = 102034};
            stu1.Report();

            Student<string> stu2 = new Student<string>() { ID = "Mike" };
            stu2.Report();
        }
    }
    interface IUnique<TId>
    {
        TId ID { get; set; }
    }
    class Student<TId> : IUnique<TId>
    {
        public TId ID { get; set; }
        public void Report()
        {
            Console.WriteLine("I am a student.My ID is {0}.",ID);
        }
    }
}

二、Partial类

允许我们把一个类的代码分成多个类、且可以用不同的语言来编写。这些被分成的多个类可以以不同的速度进行版本更新。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值