那些网站是做金融行业,php网站端口,建站宝盒如何使用,重庆互联网怎么样1、泛型 文章目录 1、泛型1、泛型是什么2、泛型分类3、泛型类和接口4、泛型方法5、泛型的作用思考 泛型方法判断类型 2、泛型约束1、什么是泛型2、各泛型约束3、约束的组合使用4、多个泛型有约束思考1 泛型实现单例模式思考2 ArrayList泛型实现增删查改 1、泛型是什么
泛型实现…1、泛型 文章目录 1、泛型1、泛型是什么2、泛型分类3、泛型类和接口4、泛型方法5、泛型的作用思考 泛型方法判断类型 2、泛型约束1、什么是泛型2、各泛型约束3、约束的组合使用4、多个泛型有约束思考1 泛型实现单例模式思考2 ArrayList泛型实现增删查改 1、泛型是什么
泛型实现了类型参数化达到代码重用目的
通过类型参数化来实现同一份代码上操作多种类型泛型相当于类型占位符
定义类或方法时使用替代符代表变量类型
当真正使用类或方法时再具体指定类型2、泛型分类
泛型类和泛型接口基本语法class 类名泛型占位字母interface 接口名泛型占位字母泛型函数基本语法函数名类型占位字母(参数列表)
//泛型占位字母可以有多个用逗号分开3、泛型类和接口
TestClassint t new TestClassint();
t.value 1;TestClassstring t2 new TestClassstring();
t2.value ok;TestClass2int, string, float, bool t3 new TestClass2int, string, float, bool();
class TestClassT
{public T value;
}
class TestClass2T, M, L,Key
{public T Value;public M GetM;public L GetL;public Key GetKey;
}
interface TestInsterfaceT
{T vale{get;set;}
}
class Test : TestInsterfaceint
{public int vale { get; set ;}
}4、泛型方法
1、普通类中的泛型方法Test2 test2 new Test2();test2.Funstring(ok);class Test2{public void FunT(T val){Console.WriteLine(val);}public void FunT(){//用泛型类型做一些逻辑处理T t default(T);}public T FunT(string test){return default(T);}public void FunT,K,M(T t, K k, M m){}}2、泛型类中的泛型方法Test2int test3 new Test2int();test3.Fun(1.2f);test3.Fun(true);test3.Fun(10);class Test2T{public T value;//这个不是泛型方法因为T是泛型类声明的时候就指定类型了public void Fun(T t){}public void FunT(T t) { }}5、泛型的作用
1、不同类型对象的相同逻辑处理就可以使用泛型
2、使用泛型可以一定程度避免装箱拆箱
例如优化ArrayList
class ArrayListT
{private T[] array;public void Add(T value){}public void Remove(T value){}
}思考 泛型方法判断类型
//定义一个泛型方法方法内判断该类型为何类型并返回类型的名称与占有的字节数
//如果是int则返回整形4字节
//只考虑int,char,float,string如果是其他类型则返回其他类型
//可以通过typeof(类型) typeof(类型)的方式进行类型判断
Console.WriteLine(Funint());
Console.WriteLine(Funchar());
Console.WriteLine(Funfloat());
Console.WriteLine(Funstring());
Console.WriteLine(Funbool());
Console.WriteLine(Funuint());string FunT()
{if (typeof(T) typeof(int)){return string.Format({0}{1}字节,整形,sizeof(int));}else if (typeof(T) typeof(char)){return string.Format({0}{1}字节, 字符, sizeof(char));}else if (typeof(T) typeof(float)){return string.Format({0}{1}字节, 单精度浮点数, sizeof(float));}else if (typeof(T) typeof(string)){return 字符串;}return 其他类型;
}2、泛型约束
1、什么是泛型
让泛型的类型有一定的限制 where1、值类型 where 泛型字母:stuct2、引用类型 where 泛型字母:class3、存在无参公共构造函数 where 泛型字母:new()4、某个类本身或其派生类 where 泛型字母:类名5、某个接口的派生类型 where 泛型字母:接口名6、另一个泛型类型本身或者派生类 where 泛型字母a:泛型字母b 2、各泛型约束
1、值类型 Test1int test1 new Test1int();test1.TestFun(1.2f);class Test1T where T : struct{public T value;public void TestFunK(K k) where K : struct{}}
2、引用类型Test2Random t2 new Test2Random();t2.value new Random();t2.TestFun(new Object());class Test2T where T : class{public T value;public void TestFunK(K k) where K : class { }}
3、存在无参公共构造函数Test3Test1 t3 new Test3Test1();Test3Test2 t4 new Test3Test2();//必须是具有公共的无参构造函数的非抽象类型class Test3T where T : new(){public T value;public void TestFunK(K k) where K : new() { }}class Test1 { }class Test2 {public Test2(int i) { }}
4、类约束Test4Test1 t4 new Test4Test1();Test4Test2 t5 new Test4Test2();class Test4T where T : Test1{public T value;public void TestFunK(K k) where K : Test1 { }}class Test1 { }class Test2 : Test1{public Test2(int i) { }}
5、接口约束Test5IFoo t6 new Test5IFoo();Test5Test1 t5 new Test5Test1();class Test5T where T : IFoo{public T value;public void TestFunK(K k) where K : IFoo { }}interface IFoo { }class Test1 : IFoo{ }
6、另一个泛型约束Test5Test1,IFoo t6 new Test5Test1,IFoo();Test5Test1, Test1 t7 new Test5Test1, Test1();class Test5T,U where T : U{public T value;public void TestFunK,V(K k) where K : V { }}interface IFoo { }class Test1 : IFoo { }3、约束的组合使用
class Test7T where T : class,new(){}4、多个泛型有约束
class Test8T,K where T:class,new() where K:struct{}思考1 泛型实现单例模式
//用泛型实现一个单例模式基类Test.Instance.value 2;
GameMgr.Instance.value 3;
class SingleBaseT where T : new()
{private static T instance new T();public static T Instance{get{return instance;}}
}
class GameMgr : SingleBaseGameMgr
{public int value 10;}
class Test
{private static Test instance new Test();public int value 10;private Test() { } public static Test Instance { get { return instance;} }
}思考2 ArrayList泛型实现增删查改
//利用泛型知识点仿造ArrayList实现一个不确定数组类型的类
//实现增删查改方法
ArrayListint array new ArrayListint();
Console.WriteLine(array.Count);
Console.WriteLine(array.Capacity);
array.Add(1);
array.Add(2);
array.Add(4);
Console.WriteLine(array.Count);
Console.WriteLine(array.Capacity);Console.WriteLine(array[1]);
Console.WriteLine(array[3]);array.Remove(2);
Console.WriteLine(array.Count);
for (int i 0; i array.Count; i)
{Console.WriteLine(array[i]);
}array[0] 88;
Console.WriteLine(array[0]);
ArrayListstring array2 new ArrayListstring();class ArrayListT
{private T[] array;//当前存了多少数private int count;public ArrayList(){count 0;//开始容量为16array new T[16];}public void Add(T value){//是否要扩容if (count Capacity){//每次扩容两倍T[] newArray new T[Capacity * 2];for (int i 0; i Capacity; i){newArray[i] array[i];}//重写指向地址array newArray;}//不需要扩容array[count] value;}public void Remove(T value){int index -1;//遍历存的值而不是数组的容量for (int i 0; i Count; i){if (array[i].Equals(value)){index i;break;}}if (index ! -1){RemoveAt(index);}}public void RemoveAt(int index){if (index 0 || index Count){Console.WriteLine(索引不合法);return;}//删除后将空出来的位置前移for (; index Count - 1; index){array[index] array[index 1];}//把最后剩下的位置设为默认值array[Count - 1] default(T);count--;}public T this[int index]{get{if (index 0 || index Count){Console.WriteLine(索引不合法);return default(T);}return array[index];}set{if (index 0 || index Count){Console.WriteLine(索引不合法);return;}array[index] value;}}/// summary/// 获取容量/// /summarypublic int Capacity{get{return array.Length;}}/// summary/// 得到具体存了多少值/// /summarypublic int Count{get{return count;}}
}