C#泛型的使用

摘录

泛型:把类的行为提取或重构出来,使之不仅能应用到他们编码的数据类型上,而且还能应用到其他类型上,重构代码并且额外增加一个抽象层,能为多段代码在不同的数据类型上执行相同指令的情况
在这里插入图片描述
在这里插入图片描述
不使用泛型,每新增类型都会重复拷贝复制代码,并修改对应的字段,很容易出错。占据额外空间,调试维护很复杂。
类型占位符定义类,创建时提供真实类型
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
由于泛型类不会知道他们保存的项的类型是什么,它不会知道这些类型实现的成员。如果使用object性质以外的的操作,就会报错。我们需要提供额外的信息让编译器知道参数可以接受哪些类型,即约束(constrain)。参数要符合约束。
在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TestGenaer : MonoBehaviour
{

    void Start()
    {

        ActionCS<int> actionCSint = new ActionCS<int>();
        actionCSint.Push(2);
        actionCSint.Push(3);
        actionCSint.Push(5);
        actionCSint.Push(7);
        actionCSint.Push(1);
        actionCSint.Push(4);
        actionCSint.Show();
        int temp = actionCSint.Pop();
        Debug.Log("temp:" + temp);
        Debug.Log("actionCSint.Learn(2,3):" + actionCSint.Learn(2, 3));
        Debug.Log("actionCSint.Learn(2,2):" + actionCSint.Learn(2, 2));
        ActionCS<string> actionCSstr = new ActionCS<string>();
        actionCSstr.Push("hahaha");
        actionCSstr.Push("heihie");
        actionCSstr.Push("ppppp");
        actionCSstr.Show();
        string temp1 = actionCSstr.Pop();
        Debug.Log("temp1:" + temp1);
        Debug.Log("actionCSstr.LearnHA HA:" + actionCSstr.Learn("hahaha", "hahaha"));
        Debug.Log("actionCSstr.LearnHA HA:" + actionCSstr.Learn("hahaha", "hahahah"));
        Debug.Log("actionCSstr.LearnHA HA:" + actionCSstr.Learn("hahaha", "haha"));
        Debug.Log("actionCSstr.LearnHA HA:" + actionCSstr.Learn("hahaha", "hahahb"));
        Debug.Log("actionCSint.MyFunc:" + actionCSint.MyFunc(actionCSstr));
        actionCSstr.Func<string, string>("1", "4");
        actionCSstr.Func1(5);
        int[] aaaaa = new int[] { 3, 6, 9 };
        actionCSstr.Func2<int>(aaaaa);
        actionCSstr.Func2(aaaaa);
        string[] sssss = new string[] { "aaa", "bbb", "ccc" };
        actionCSstr.Func2<string>(sssss);
        actionCSstr.Func2(sssss);
        Holder<int> holder = new Holder<int>(3, 4, 5);
        ExtHolder.ExtShow(holder);
        // holder.GetVals();
        // holder.ExtShow();
        var hjhk = new PieceOf<string>("hi i am here");
        var uiuoo = new PieceOf<int>(89789);
        Debug.Log("hjhk.Data:" + hjhk.Data);
        Debug.Log("uiuoo.Data:" + uiuoo.Data);
        Simper simper = new Simper();

        MyDele<string, int, int> myDele = new MyDele<string, int, int>(simper.MyFunc);
        myDele += simper.MyFunc1;
        Debug.Log(myDele(55, 66));
        simper.GetT(111);
        simper.GetT("ryry");
        SimperInter<string> simperInter = new SimperInter<string>();
        Debug.Log("simperInter.GetT(tyuio):" + simperInter.GetT("tyuio"));

    }


}


public class SimperInter<T> : IType<T>
{
    T t;
    public T GetT(T t)
    {

        this.t = t;
        return t;
    }
}
public interface IType<T>
{
    T GetT(T t);
}
public delegate T1 MyDele<T1, T2, T3>(T2 t2, T3 t3);

public class Simper : IType<int>, IType<string>
{
    public string MyFunc(int i1, int i2)
    {
        return (i1 + i2).ToString();
    }

    public string MyFunc1(int i1, int i2)
    {
        return ("ttt" + (i1 + i2).ToString()).ToUpper();
    }

    public int GetT(int t)
    {
        return t;
    }

    public string GetT(string t)
    {
        return t;
    }



    public Simper()
    {

        MyDele<string, int, int> myDele = new MyDele<string, int, int>(MyFunc);//绑进来的方法要不是是静态的,要不是实例化的, 如果写在 类的内部声明, 绑定当前类中的非静态方法,是不可以的,因为 ,该类并未被实例化,方法也不是静态的, 委托初始化对象找不到对应的类型,  除非把初始方法设置为静态的,或者 把 初始化委托写在构造里面, 因为  在构造里时,说明 该类已经被实例化了.
    }

}
struct PieceOf<T>
{
    public PieceOf(T value) { _data = value; }
    private T _data;
    public T Data { get { return _data; } set { _data = value; } }

}

static class ExtHolder
{
    public static void ExtShow<O>(this Holder<O> holder)
    {
        O[] os = holder.GetVals();
        foreach (var item in os)
        {
            Debug.Log("OS:" + item);
        }
    }
}

class Holder<U>
{
    U[] vals = new U[3];
    public Holder(U v0, U v1, U v2)
    {
        vals[0] = v0;
        vals[1] = v1;
        vals[2] = v2;
    }

    public U[] GetVals()
    {
        return vals;
    }
}

public class ActionCS<T> where T : IComparable//对对象进行实例化可以用泛型,就不用那么多switch,泛型对象池,大家的操作都是相同的,只是操作的类型不同,但如果大家的操作都有点区别,有的多有的少,还有的不一样的,那就该用继承抽象或者是接口
{
    public T[] array;
    int count = 0;


    const int maxNum = 5;
    public ActionCS()
    {
        array = new T[maxNum];
    }
    public void Push(T args)
    {
        if (count < maxNum) array[count++] = args;
    }

    public string MyFunc<S>(S temp)
    {
        return temp.ToString();
    }

    public void Func<H2, H3>(H2 h2, H3 h3)
    {
        H2 h21 = h2;
        H3 h31 = h3;
    }

    public void Func1<H4>(H4 h4)
    {
        H4 h41 = h4;
    }

    public void Func2<H5>(H5[] arry)
    {
        Array.Reverse(arry);
        foreach (H5 item in arry)
        {
            Debug.Log("item:" + item);
        }
    }
    public int Learn(T t1, T t2)
    {
        return t1.CompareTo(t2);//以出现的第一个不相同的字符做比较Ascii码作比较,值较大的则大返回1,反之返回-1
    }
    public T Pop()
    {
        return count != 0 ? array[--count] : array[0];
    }

    public void Show()
    {
        foreach (var item in array)
        {
            Debug.Log("item:" + item);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值