C#核心知识回顾——9.泛型、泛型约束

1.泛型

    泛型实现了类型参数化,达到代码重用目的
    通过类型参数化来实现同一份代码上操作多种类型

    泛型相当于类型占位符
    定义类或方法时使用替代符代表变量类型
    当真正使用类或者方法时再具体指定类型

    泛型类和泛型接口
    基本语法:
    class类名<泛型占位字母>
    interface接口名<泛型占位字母>

public class test : MonoBehaviour
{
    private void Start()
    {
        TestClass<int> testClass = new TestClass<int>();
        Debug.Log(testClass.value);
        TestClass2<int,string,bool> testclass2 = new TestClass2<int,string,bool>();
        Debug.Log(testclass2.value);
        
    }
}

class TestClass<T>
{
    public T value;
}
class TestClass2<T, K, M>
{
    public K key;
    public M value;
    public T Value { get; set; }
}
public interface TestInterface<T>
{
     T Value { get; set; }
}
class TestGetInter : TestInterface<int>
{
    public int Value { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}

    泛型函数
    基本语法:函数名<泛型占位字母>(参数列表)

    注意:泛型占位字母可以有多个,用逗号分开

/// <summary>
    /// 普通类里面的泛型方法
    /// </summary>
    public void testFun<T>(T value)
    {
        Debug.Log(value);
    }
    public void testFunc<T>()
    {
        //用泛型做逻辑处理
        T t = default(T);
    }
    public T testFun<T>()
    {
        return default(T);
    }

    private void Start()
    {
        testFun<string>("12");        
    }
/// <summary>
/// 泛型类中的泛型方法
/// </summary>
public class test : MonoBehaviour
{    
    private void Start()
    {
        Test2<int> test = new Test2<int>();
        //被限定了类型。函数只能传int
        test.Func(1);
        //Func2是真正的泛型方法
        test.Func2("123");
        test.Func2(false);
    }
}
class Test2<T>
{
    public T value;
    /// <summary>
    /// 这个不叫泛型方法,T在类已经定性了。
    /// </summary>
    public void Func<T>(T t) { Debug.Log(t); }
    /// <summary>
    /// 这个才是泛型方法
    /// </summary>
    public void Func2<K>(K k) { }
}

2.泛型约束

让泛型的类型有一定的限制
关键字:where
泛型约束一共有6种
1.值类型                                                   where 泛型字母:struct
2.引用类型                                               where 泛型字母:class
3.存在无参公共构造函数                          where 泛型字母:new()
4.某个类本身或者其派生类                      where 泛型字母: 类名
5.某个接口的派生类型                             where 泛型字母:接口名
6.另一个泛型类型本身或者派生类型       where 泛型字母:另一个泛型字母

/// <summary>
/// 泛型类中的泛型方法
/// </summary>
public class test : MonoBehaviour
{    
    private void Start()
    {
        Test2<int> t1= new Test2<int>();                        值类型
        t1.TestFunc<float> (1.3f);

        Test3<System.Random> t2 = new Test3<System.Random>();   引用类型
        t2.TestFunc<int[]>(new int[5]);

        //<Get2>必须是公共的无参数构造函数的非抽象类型
        Test4<Get2> t3 = new Test4<Get2>();                     无参构造函数

        Test5<Get2> t5 = new Test5<Get2>();                     类约束

        Test6<IGet3> t6 = new Test6<IGet3>();                   接口约束

        Test7<IGet4,IGet3> t7 = new Test7<IGet4,IGet3>();       另一个泛型约束
        //IGet4继承于IGet3
    }
}
class Get2
{
    public Get2(int a) { }
    public Get2() { }
}
interface IGet3
{
}
interface IGet4 : IGet3
{
}
/// <summary>
/// 值类型约束
/// </summary>
class Test2<T> where T : struct
{
    public T value;
    public void TestFunc<K>(K k) where K : struct { }
}
/// <summary>
/// 引用类型约束
/// </summary>
class Test3<T> where T : class
{
    public T value;
    public void TestFunc<K>(K k) where K : class { }
}
/// <summary>
/// 无参构造函数
/// </summary>
class Test4<T> where T : new()
{
    public T value;
    public void TestFunc<K>(K k) where K : struct{ }
}
/// <summary>
/// 类约束
/// </summary>
class Test5<T> where T : Get2  //某个类或者其派生类
{
    public T value;
    public void TestFunc<K>(K k) where K : struct { }
}
/// <summary>
/// 接口约束
/// </summary>
class Test6<T> where T : IGet3  //某个接口或者其派生类/派生接口
{
    public T value;
    public void TestFunc<K>(K k) where K : struct { }
}
/// <summary>
/// 另一个泛型约束
/// </summary>
class Test7<T,U> where T : U  //另一个泛型本身或者派生类
{
    public T value;
    public void TestFunc<K>(K k) where K : struct { }
}

约束的组合使用:

//new()要放在后面
class Test3<T> where T : class,new()
{
    public T value;
    public void TestFunc<K>(K k) where K : class { }
}

多个泛型有约束:

class Test2<T,M> where T : struct where M : struct
{
    public T value;
    public void TestFunc<K>(K k) where K : struct { }
}

猜你喜欢

转载自blog.csdn.net/qq_29296473/article/details/131536896