今天讲了泛类型
就是这个类型代表任何的类型,呵呵,挺奇妙
下面附上我上课时跟着写的代码吧
这是一个类:
public class Point<T> //泛类型
{
private T xPos;
private T yPos;
public T X
{
get { return this.xPos; }
set { this.xPos = value; }
}
public T Y
{
get { return this.yPos; }
set { this.yPos = value; }
}
public override string ToString()
{
return string.Format("X:{0}Y:{1}",xPos,yPos);
}
public Point(T x,T y)
{
xPos = x;
yPos = y;
}
}
这是一个方法
protected void Page_Load(object sender, EventArgs e)
{
int a = 10;
int b = 20;
//Swap(ref a,ref b);
this.Swap(ref a, ref b);
Response.Write(string.Format("a:{0};b:{1}", a, b));
}
public void Swap(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
public void Swap(ref float a, ref float b)
{
float temp;
temp = a;
a = b;
b = temp;
}
public void Swap<T>(ref T a, ref T b) //泛型方法
{
T temp;
temp = a;
a = b;
b = temp;
}
2011-11-8
最新推荐文章于 2022-02-18 15:13:54 发布