泛型杂项

C#编程 · 06-22 · 186 人浏览

public class fanxingclass : MonoBehaviour
{

private class Data<T>
{
    public T[] array;
    public Data()
    {
        this.array = new T[10];
    }
    public void set(T newvalue,int index)
    {
        array[index] = newvalue;
    }
    public void SetD<K>(T newvalue, int index, K ooo) where K:MonoBehaviour
    {
        set(newvalue, index);
        Debug.Log(ooo);
    }
}
    
// Start is called before the first frame update
void Start()
{
    Data<int> d1 = new Data<int>();
    d1.set(1, 0);
    int num = d1.array[0];
    Debug.Log(num);

    Data<float> d2 = new Data<float>();
    d2.set(3.14f, 0);
    float num2 = d2.array[0];
    Debug.Log(num2);
    fanxin f = new fanxin();
    d2.SetD<fanxin>(1.3f, 111, f);
}

// Update is called once per frame
void Update()
{
    
}

}

Theme Jasmine by Kent Liao