泛型类功能
/*
----泛型---
性能:泛型类型在JIT时不在进行装箱拆箱
类型安全性:定义是什么类型就必须放入什么类型
二进制代码重用:可以在.NET中其他语言重用
代码的扩展:
命名约定:在定义泛型类的时候最好使用T进行类型替代。
泛型类功能
默认值
约束
继承
静态成员
*/
using System.Collections;
using System.Collections.Generic;
namespace Frank
{
public class Test
{
public static void Main(string[] args)
{
}
}
}
public interface ITest2
{
}
//创建泛型类
public class LinkedListNode<T>
where T : ITest2,new()//定于约束 传进来的类型必须继承后面的类型
{
private readonly LinkedListNode<T> queue = new LinkedListNode<T>();
public void AddDocument(T doc)
{
lock(this)
{
//添加到队列
}
}
public bool IsAvailable
{
get
{
return 1 > 0;
}
}
public T Get()
{
T doc = default(T);//定义默认值 将null赋值引用类型,0赋值为值类型
lock(this)
{
//doc=...
}
return doc;
}
}
约束列表:
泛型中的静态字段:相同的类型它们共享一个静态字段。LinkedListNode<int> LinkedListNode<string> 它们共享两个不同的静态字段。
浙公网安备 33010602011771号