using System;
using System.Collections.Generic;
using System.Text;
namespace DicrionaryTest
{
public class Stack<T>//定义一个泛型类
{
private int count;//元素个数
private T[] items;//用T替换一个具体的数据类型
public Stack(int size)
{
items = new T[size];//使用泛型
count = 0;
}
public void Push(T k)
{
items[count++] = k;
}
public T Pop()//采用泛型作为类型
{
return items[--count];
}
public int Count//只读属性
{
get
{
return this.count;
}
}
}
class Test
{
static void Main(string[] args)
{
Stack<int> ts = new Stack<int>(10);//定义一个存放int型数据的栈
ts.Push(123);//进栈
ts.Push(456);
string str = "";
while (ts.Count > 0)
{
str = str + ts.Pop() + "\t";//出栈
}
Console.WriteLine(str);
Console.ReadLine();
}
}
}