• 十三、泛型


    1、泛型

    • class 类名
    • interface 接口名
    • 函数名(...)
    • 泛型可以有多个,用 , 分开
    // 泛型类
    class MyTest<T>
    {
        public T Value;
        
        // 这个不是泛型方法哦(只有函数名后面有<>的才是)
        public void Test(T t)
        {
            // ...
        }
    }
    
    // 普通类
    class MyTest
    {
        // 泛型方法
        public void Test<T>(T t)
        {
            Console.WriteLine(t);
        }
        
        // 泛型方法
        public void Test<T>()
        {
            T t = default(T);
            // ...
        }
    }
    
    // 泛型接口
    interface ICommon<T>
    {
        T Value
        {
            set;
            get;
        }
    }
    
    // 普通类继承泛型接口要指定泛型具体类型
    class MyCommonImpl : ICommon<int>
    {
        // ...
    }
    
    // 泛型类与泛型接口所用泛型相同则可不指定接口泛型具体类型
    class MyCommonImpl02<T> : ICommon<T>
    {
        // ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    2、泛型约束

    • 关键字:where
    • 一共 6 种约束
      • 值类型:where 泛型字母:struct
      • 引用类型:where 泛型字母:class
      • 存在无参公共构造函数的非抽象类型:where 泛型字母:new()
      • 某个类本身或其派生类:where 泛型字母:类名
      • 某个接口的派生类:where 泛型字母:接口名
      • 另一个泛型类型本身或其派生类型:where 泛型字母:另一个泛型类型字母

    2.1、值类型

    class Test<T> where T:struct
    {
        public T Value;
    
        public void TestFun<K>(K k) where K:struct
        {
            Console.WriteLine(k);
        }
    }
    
    // 某方法内↓↓↓
    Test<int> test = new Test<int>();
    test.Value = 10;
    test.TestFun(test.Value); // 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.2、引用类型

    class Test<T> where T:class
    {
        public T Value;
    
        public void TestFun<K>(K k) where K:class
        {
            Console.WriteLine(k);
        }
    }
    
    // 某方法内↓↓↓
    Test<string> test = new Test<string>();
    test.Value = "嘎嘎";
    test.TestFun(test.Value); // "嘎嘎"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.3、存在无参公共构造函数的非抽象类型

    class Test<T> where T:new()
    {
        public T Value;
    
        public void TestFun<K>(K k) where K:new()
        {
            Console.WriteLine(k);
        }
    }
    
    class MyCons
    {
        public override string ToString()
        {
            return "这是一个有无参构造的类的ToString()";
        }
    }
    
    // 某方法内↓↓↓
    Test<MyCons> test = new Test<MyCons>();
    test.Value = new MyCons();
    test.TestFun(test.Value); // "这是一个有无参构造的类的ToString()"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2.4、某个类本身或其派生类

    class Test<T> where T:MyCons
    {
        public T Value;
    
        public void TestFun<K>(K k) where K:MyCons
        {
            Console.WriteLine(k);
        }
    }
    
    class MyCons
    {
        public override string ToString()
        {
            return "这是MyCons的ToString()";
        }
    }
    
    // 某方法内↓↓↓
    Test<MyCons> test = new Test<MyCons>();
    test.Value = new MyCons();
    test.TestFun(test.Value); // "这是MyCons的ToString()"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2.5、某个接口的派生类

    class Test<T> where T : ITest
    {
        public T Value;
    
        public void TestFun<K>(K k) where K : ITest
        {
            Console.WriteLine(k);
        }
    }
    
    interface ITest
    { }
    
    class MyTest : ITest
    {
        public override string ToString()
        {
            return "这是 ITest 实现类 MyTest 的ToString()";
        }
    }
    
    // 某方法内↓↓↓
    Test<ITest> test = new Test<ITest>();
    test.Value = new MyTest();
    test.TestFun(test.Value); // "这是 ITest 实现类 MyTest 的ToString()"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2.6、另一个泛型类型本身或其派生类型

    class Test<T, U> where T : U
    {
        public T ValueT;
    
        public U ValueU;
    
        public void TestFun<K, V>(K k, V v) where K : V
        {
            Console.WriteLine("{0}{1}", k, v);
        }
    }
    
    interface ITest
    { }
    
    class MyTest : ITest
    {
        public override string ToString()
        {
            return "这是 ITest 实现类 MyTest 的ToString()";
        }
    }
    
    class YourTest : ITest
    {
        public override string ToString()
        {
            return "这是 ITest 实现类 YourTest 的ToString()";
        }
    }
    
    // 某方法内↓↓↓
    Test<ITest, ITest> test = new Test<ITest, ITest>();
    test.ValueT = new MyTest();
    test.ValueU = new YourTest();
    test.TestFun(test.ValueT, test.ValueU);
    // 这是 ITest 实现类 MyTest 的ToString()这是 ITest 实现类 YourTest 的ToString()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    3、约束组合使用

    • 不冲突的情况下随意组合
    class Test<T> where T:class, new()
    {}
    
    • 1
    • 2

    4、多个泛型约束

    • 罗列 where 就完了(别随手加逗号啥的,空开即可)
    class Test<T, V> where T:class, new() where V:struct
    {}
    
    • 1
    • 2
  • 相关阅读:
    android 解决AVC问题
    【Tensorflow 2.12 电影推荐系统之召回模型】
    Varnish 6-2-0-porting-guide
    关于网页中的文本选择以及统计选中文本长度
    php表单提交并自动发送邮件给某个邮箱(示例源码下载)
    基于51单片机交通信号灯仿真_东西管制+南北管制
    校招失败,在小公司熬了 2 年后我终于进了阿里,竭尽全力(Java 岗)
    【论文翻译】使用变更数据捕获方法通过提取-转换-加载过程实时更新数据仓库
    百度百科创建基础概念分享,为什么百度百科那么难做
    vscode连接到WSL子系统报错/bin/ea1445cc7016315d0f5728f8e8b12a45dc0a7286/node: not found
  • 原文地址:https://blog.csdn.net/qq_30769437/article/details/128194615