泛型,泛型约束

嚴華  金牌会员 | 2024-12-27 18:57:20 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

什么是泛型?

泛型(Generics) 是一种编程技能,广泛应用于面向对象编程(OOP)中,特殊是在C#、Java等现代编程语言中。泛型的核心思想是创建可以在多种数据范例上工作的类、接口和方法,而不需要为每种数据范例单独编写代码。这不但进步了代码的重用性,还增强了范例安全性,减少了运行时错误。
泛型的主要优点

  • 范例安全性:

    • 泛型在编译时举行范例检查,确保范例的一致性,减少运行时范例转换错误。

  • 代码重用:

    • 通过泛型,可以编写适用于多种数据范例的通用代码,而不需要为每种数据范例编写单独的实现。

  • 性能提升:

    • 泛型避免了装箱(boxing)和拆箱(unboxing)操作,进步了性能,特殊是在处理值范例时。

  • 可读性和可维护性:

    • 泛型代码更易于阅读和维护,因为范例信息在编译时就已经明确。

泛型的实现
<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. }
复制代码
泛型约束:
可以为泛型类、接口和方法添加约束,以限制泛型参数的范例。
常见的约束包括:

  • where T : new():要求泛型参数必须有一个无参构造函数。
  • where T : class:要求泛型参数必须是引用范例。
  • where T : struct:要求泛型参数必须是值范例。
  • where T : IInterface:要求泛型参数必须实现特定的接口。
  • where T : BaseClass:要求泛型参数必须继续自特定的基类。
    1.   public class GenericConstraint<T> where T : new()
    2.   {
    3.           public T CreateInstance()
    4.           {
    5.                   return new T();
    6.           }
    7.   }
    8.   public class MyClass
    9.   {
    10.           public void MyMethod()
    11.           {
    12.                   Console.WriteLine("MyClass instance created.");
    13.           }
    14.   }
    15.   public class Program
    16.   {
    17.           public static void Main()
    18.           {
    19.                   GenericConstraint<MyClass> constraint = new GenericConstraint<MyClass>();
    20.                   MyClass instance = constraint.CreateInstance();
    21.                   instance.MyMethod(); // 输出: MyClass instance created.
    22.           }
    23.   }
    复制代码

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

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

嚴華

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表