ToB企服应用市场:ToB评测及商务社交产业平台

标题: 泛型,泛型约束 [打印本页]

作者: 嚴華    时间: 2024-12-27 18:57
标题: 泛型,泛型约束
什么是泛型?

泛型(Generics) 是一种编程技能,广泛应用于面向对象编程(OOP)中,特殊是在C#、Java等现代编程语言中。泛型的核心思想是创建可以在多种数据范例上工作的类、接口和方法,而不需要为每种数据范例单独编写代码。这不但进步了代码的重用性,还增强了范例安全性,减少了运行时错误。
泛型的主要优点
泛型的实现
<ol>泛型类:
定义一个类,使其可以处理多种数据范例的实例。
  1. public class Box<T>
  2. {
  3.         private T _content;
  4.         public void SetContent(T content)
  5.         {
  6.                 _content = content;
  7.         }
  8.         public T GetContent()
  9.         {
  10.                 return _content;
  11.         }
  12. }
  13. public class Program
  14. {
  15.         public static void Main()
  16.         {
  17.                 Box<int> intBox = new Box<int>();
  18.                 intBox.SetContent(42);
  19.                 Console.WriteLine(intBox.GetContent()); // 输出: 42
  20.                 Box<string> stringBox = new Box<string>();
  21.                 stringBox.SetContent("Hello, World!");
  22.                 Console.WriteLine(stringBox.GetContent()); // 输出: Hello, World!
  23.         }
  24. }
复制代码
泛型接口:
定义一个接口,使其可以处理多种数据范例的实例。
  1. public interface IStorage<T>
  2. {
  3.         void Store(T item);
  4.         T Retrieve();
  5. }
  6. public class Storage<T> : IStorage<T>
  7. {
  8.         private T _item;
  9.         public void Store(T item)
  10.         {
  11.                 _item = item;
  12.         }
  13.         public T Retrieve()
  14.         {
  15.                 return _item;
  16.         }
  17. }
  18. public class Program
  19. {
  20.         public static void Main()
  21.         {
  22.                 IStorage<int> intStorage = new Storage<int>();
  23.                 intStorage.Store(100);
  24.                 Console.WriteLine(intStorage.Retrieve()); // 输出: 100
  25.                 IStorage<string> stringStorage = new Storage<string>();
  26.                 stringStorage.Store("Generic Storage");
  27.                 Console.WriteLine(stringStorage.Retrieve()); // 输出: Generic Storage
  28.         }
  29. }
复制代码
泛型方法:
定义一个方法,使其可以处理多种数据范例的参数和返回值。
  1. public class GenericMethods
  2. {
  3.         public static T GetDefault<T>() where T : new()
  4.         {
  5.                 return new T();
  6.         }
  7.         public static void Print<T>(T item)
  8.         {
  9.                 Console.WriteLine(item);
  10.         }
  11. }
  12. public class Program
  13. {
  14.         public static void Main()
  15.         {
  16.                 int defaultInt = GenericMethods.GetDefault<int>();
  17.                 Console.WriteLine(defaultInt); // 输出: 0
  18.                 string defaultString = GenericMethods.GetDefault<string>();
  19.                 Console.WriteLine(defaultString); // 输出: (空字符串)
  20.                 GenericMethods.Print(42); // 输出: 42
  21.                 GenericMethods.Print("Hello, World!"); // 输出: Hello, World!
  22.         }
  23. }
复制代码
泛型约束:
可以为泛型类、接口和方法添加约束,以限制泛型参数的范例。
常见的约束包括:

泛型的应用场景
1.集合类:
泛型集合类(如List




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4