让泛型的类型有一定的限制 where1、值类型 where 泛型字母:stuct2、引用类型 where 泛型字母:class3、存在无参公共构造函数 where 泛型字母:new()4、某个类本身或其派生类 where 泛型字母:类名5、某个接口的派生类型 where 泛型字母:接口名6、另一个泛型类型本身或者派生类 where 泛型字母a:泛型字母b
2、各泛型约束
1、值类型 Test1<int> test1 =newTest1<int>();test1.TestFun(1.2f);classTest1<T> where T: struct{publicT value;publicvoidTestFun<K>(K k) where K: struct{}}2、引用类型Test2<Random> t2 =newTest2<Random>();t2.value =newRandom();t2.TestFun(newObject());classTest2<T> where T:class{publicT value;publicvoidTestFun<K>(K k) where K:class{}}3、存在无参公共构造函数Test3<Test1> t3 =newTest3<Test1>();Test3<Test2> t4 =newTest3<Test2>();//必须是具有公共的无参构造函数的非抽象类型classTest3<T> where T:new(){publicT value;publicvoidTestFun<K>(K k) where K:new(){}}classTest1{}classTest2{publicTest2(int i){}}4、类约束Test4<Test1> t4 =newTest4<Test1>();Test4<Test2> t5 =newTest4<Test2>();classTest4<T> where T:Test1{publicT value;publicvoidTestFun<K>(K k) where K:Test1{}}classTest1{}classTest2:Test1{publicTest2(int i){}}5、接口约束Test5<IFoo> t6 =newTest5<IFoo>();Test5<Test1> t5 =newTest5<Test1>();classTest5<T> where T:IFoo{publicT value;publicvoidTestFun<K>(K k) where K:IFoo{}}interfaceIFoo{}classTest1:IFoo{}6、另一个泛型约束Test5<Test1,IFoo> t6 =newTest5<Test1,IFoo>();Test5<Test1,Test1> t7 =newTest5<Test1,Test1>();classTest5<T,U> where T:U{publicT value;publicvoidTestFun<K,V>(K k) where K:V{}}interfaceIFoo{}classTest1:IFoo{}
3、约束的组合使用
classTest7<T> where T:class,new(){}
4、多个泛型有约束
classTest8<T,K> where T:class,new() where K:struct{}
思考1 泛型实现单例模式
//用泛型实现一个单例模式基类Test.Instance.value =2;GameMgr.Instance.value =3;classSingleBase<T> where T:new(){privatestaticT instance =newT();publicstaticTInstance{get{return instance;}}}classGameMgr:SingleBase<GameMgr>{publicint value =10;}classTest{privatestaticTest instance =newTest();publicint value =10;privateTest(){}publicstaticTestInstance{ get {return instance;}}}
思考2 ArrayList泛型实现增删查改
//利用泛型知识点,仿造ArrayList实现一个不确定数组类型的类//实现增删查改方法ArrayList<int> array =newArrayList<int>();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]);ArrayList<string> array2 =newArrayList<string>();classArrayList<T>{privateT[] array;//当前存了多少数privateint count;publicArrayList(){count =0;//开始容量为16array =newT[16];}publicvoidAdd(T value){//是否要扩容if(count >=Capacity){//每次扩容两倍T[] newArray =newT[Capacity*2];for(int i =0; i <Capacity; i++){newArray[i]= array[i];}//重写指向地址array = newArray;}//不需要扩容array[count++]= value;}publicvoidRemove(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);}}publicvoidRemoveAt(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--;}publicTthis[int index]{get{if(index <0|| index >=Count){Console.WriteLine("索引不合法");returndefault(T);}return array[index];}set{if(index <0|| index >=Count){Console.WriteLine("索引不合法");return;}array[index]= value;}}/// <summary>/// 获取容量/// </summary>publicintCapacity{get{returnarray.Length;}}/// <summary>/// 得到具体存了多少值/// </summary>publicintCount{get{return count;}}}
需要使用到的包
from collections import dequeimport cv2
import numpy as np
import math
import shutilimport sys
import os
import time#这个求出现频率最高的太慢了,所以把它放弃了
from collections import Counter准备好安装包后需要获取图片
def star():…