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; } } }
标签:Box,box,set,get,使用,泛型,new,class From: https://www.cnblogs.com/decoct-tea/p/17151530.html