C# のコア知識の復習 - 9. ジェネリック、一般的な制約

1. ジェネリック医薬品

    ジェネリックスは、コードの再利用を実現するために型のパラメーター化を実装しており、
    型のパラメーター化を通じて、同じコードを複数の型で操作できます。

    ジェネリックスは型のプレースホルダーに相当します。
    クラスまたはメソッドを定義するときに、変数の型を表すために代替が使用されます。
    クラスまたはメソッドが実際に使用されるときに、型が指定されます。

    ジェネリック クラスとジェネリック インターフェイスの
基本構文     :
    class クラス名 <ジェネリック プレースホルダー文字>
    インターフェイス インターフェイス名 <ジェネリック プレースホルダー文字>

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. 一般的な制約

ジェネリック型に特定の制限を持たせる
キーワード: ここで
ジェネリック制約は 6 種類あります
1. 値型 (ジェネリック文字: struct)
2. 参照型 (ジェネリック文字: クラス)
3. パラメータのないパブリック コンストラクタがあります (ジェネリック型文字: new) ()
4. あるクラス自体、またはその派生クラス (総称文字: クラス名)
5. インターフェイスの派生型 (総称文字: インターフェイス名)
6. 別のジェネリック型自体、または派生型 (総称文字: 別の総称文字)

/// <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