1、泛型类  避免了类型膨胀和类成员膨胀

namespace Generic
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Apple apple=new Apple() { color="red"};
            Book book=new Book() { bookName="只因前传"};
            Box<Apple> boxApple=new Box<Apple>() { box=apple};
            Box<Book> boxBook = new Box<Book>() { box = book };
            Console.WriteLine(boxApple.box.color);
            Console.WriteLine(boxBook.box.bookName);
        }
    }

    class Box<T>      //一劳永逸
    {
        public T box { get; set; }
    }

    class Apple
    {
        public string color { get; set; }
    }
    class Book
    {
        public string bookName { get; set; }
    }
}

2、泛型接口

namespace Generic
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student<int> student = new Student<int>();
            student.ID = 1;
            
        }
    }

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

    class Student<T> : IUnique<T>
    {
        public T ID { get; set; }
    }


}

 

posted on 2023-02-24 17:12  漂乎兮乎  阅读(8)  评论(0编辑  收藏  举报