您现在的位置是:主页 > news > nba最新排名公布/宁波seo营销
nba最新排名公布/宁波seo营销
admin2025/6/30 5:24:36【news】
简介nba最新排名公布,宁波seo营销,怎么用APdiv做网站导航栏,建设银行网站网址泛型:System.Collection.Generic(强类型集合)1..NET Framework2.0之后出现的一种类型安全机制2.泛型的出现,将运行时的错误转到了编译时。3.泛型,不会强行对值类型进行装箱和拆箱,或对引用类型进行强制类型转换,所以性…
nba最新排名公布,宁波seo营销,怎么用APdiv做网站导航栏,建设银行网站网址泛型:System.Collection.Generic(强类型集合)1..NET Framework2.0之后出现的一种类型安全机制2.泛型的出现,将运行时的错误转到了编译时。3.泛型,不会强行对值类型进行装箱和拆箱,或对引用类型进行强制类型转换,所以性… 一、List
- 泛型:System.Collection.Generic(强类型集合)
1..NET Framework2.0之后出现的一种类型安全机制
2.泛型的出现,将运行时的错误转到了编译时。
3.泛型,不会强行对值类型进行装箱和拆箱,或对引用类型进行强制类型转换,所以性能得到提高。
4.泛型主要有:List< T >、Dictionary、Queue< T >、Stack< T >
- 泛型中的多态:可添加子类成员
class People
{
public string name = "";
public int age;
public People()
{
}
public People(string n,int a)
{
this.name = n;
this.age = a;
}
public override string ToString()
{
return this.name + ":" + this.age;
}
}
class Student : People
{
public string sex;
public Student (string n,int a,string s):base(n,a)
{
this.sex = s;
}
}
- List类是 ArrayList 类的泛型等效类
List people_list = new List();
People p1 = new People("xiaobai", 18);
People p2 = new People("xiaohua", 28);
People p3 = new People("xiaosusu", 20);
people_list.Add(p1);
people_list.Add(p2);
people_list.Add(p3);
foreach (People item in people_list)
{
Console.WriteLine("重写ToString() "+item);
Console.WriteLine(item.name + ":" + item.age);
}
Student s1 = new Student("xiaohong", 15, "女");
people_list.Insert(0,s1);
Console.WriteLine("插入子类对象后");
foreach (People item in people_list)
{
Console.WriteLine("重写ToString() "+item);
Console.WriteLine(item.name + ":" + item.age);
}
Console.WriteLine("是否存在小苏苏?:" + people_list.Contains(p3));
结果为:
重写ToString() xiaobai:18
xiaobai:18
重写ToString() xiaohua:28
xiaohua:28
重写ToString() xiaosusu:20
xiaosusu:20
插入子类对象后
重写ToString() xiaohong:15
xiaohong:15
重写ToString() xiaobai:18
xiaobai:18
重写ToString() xiaohua:28
xiaohua:28
重写ToString() xiaosusu:20
xiaosusu:20
是否存在小苏苏?:True
- List泛型为自定义类排序,两种方法:实现IComparer接口、实现IComparable接口
1.实现IComparer接口
public class SortMgr : MonoBehaviour {
public class Bullet:IComparer
{
public Bullet()
{
distance = Random.Range(0, 100);
}
public float distance;
public int Compare(Bullet x, Bullet y)
{
return x.distance.CompareTo(y.distance);
}
}
void Start ()
{
List bulletsPool=new List();
for (int i = 0; i < 10; i++)
{
Bullet _bullet=new Bullet();
bulletsPool.Add(_bullet);
}
for (int i = 0; i < bulletsPool.Count; i++)
{
Debug.Log(i+":"+bulletsPool[i].distance);
}
IComparer b=new Bullet();
bulletsPool.Sort(b.Compare);
for (int i = 0; i < bulletsPool.Count; i++)
{
Debug.Log(i + "--" + bulletsPool[i].distance);
}
}
}
2. 实现IComparable接口
public class SortMgr : MonoBehaviour
{
public class Bullet : IComparable
{
public Bullet()
{
distance = Random.Range(0, 100);
}
public float distance;
public int CompareTo(Bullet other)
{
if (distance == other.distance)
{
return 0;
}
if (distance > other.distance)
{
return 1;
}
return -1;
}
}
void Start()
{
List bulletsPool = new List();
for (int i = 0; i < 10; i++)
{
Bullet _bullet = new Bullet();
bulletsPool.Add(_bullet);
}
for (int i = 0; i < bulletsPool.Count; i++)
{
Debug.Log(i + ":" + bulletsPool[i].distance);
}
bulletsPool.Sort();
for (int i = 0; i < bulletsPool.Count; i++)
{
Debug.Log(i + ":" + bulletsPool[i].distance);
}
}
}
- 也可以不实现接口,直接在sort()中实现: bulletsPool.Sort((a, b) => (a.distance - b.distance) == 0 ? 0 : (a.distance - b.distance) > 0 ? 1 : -1);
二、Dictionary
- Dictionary 的遍历:(KeyValuePair泛型)
- 键不可重复,不可为空;值可以为null
Dictionary people_dic = new Dictionary();
people_dic.Add(0, p1);
people_dic.Add(1, p2);
people_dic.Add(2, p3);
people_dic.Add(3, s1);
foreach (var item in people_dic)
{
Console.WriteLine("重写ToString() "+item);
Console.WriteLine(item.Key+":"+item.Value.age+" "+item.Value.name);
}
foreach (var item in people_dic.Keys)
{
Console.WriteLine(item);
}
foreach (var item in people_dic.Values)
{
Console.WriteLine(item.name);
}
结果为:
重写ToString() [0, xiaobai:18]
0:18 xiaobai
重写ToString() [1, xiaohua:28]
1:28 xiaohua
重写ToString() [2, xiaosusu:20]
2:20 xiaosusu
重写ToString() [3, xiaohong:15]
3:15 xiaohong
0
1
2
3
xiaobai
xiaohua
xiaosusu
xiaohong