using System;class GenericAList
{
static void Main(string[] args)
{
GElementList<int> theGElementList = new GElementList<int>() ;
for (int i = 1; i < 6; i++ )
theGElementList.AddE(i*10); string listValue = theGElementList.GetListData();
Console.WriteLine(listValue); GElementList<string> theGElementList1 = new GElementList<string>(); theGElementList1.AddE("A");
theGElementList1.AddE("B");
theGElementList1.AddE("C");
theGElementList1.AddE("D");
theGElementList1.AddE("E");
listValue = theGElementList1.GetListData();
Console.WriteLine(listValue);
Console.Read();
}
}
public class ElementS<T>
{
private T cElement ;
public T GetElement()
{
return cElement;
}
public void SetElement(T pElement)
{
this.cElement = pElement;
}
private ElementS<T> theElementS = null ; public ElementS<T> GetEClass()
{
return theElementS;
}
public void SetEClass(ElementS<T> pElementS)
{
this.theElementS = pElementS;
}
}
public class GElementList<T>
{
private ElementS<T> firstElement = new ElementS<T>() ;
public void AddE(T pElement)
{
ElementS<T> newElementS = new ElementS<T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
public string GetListData()
{
string returnValue = "" ;
do
{
returnValue = firstElement.GetElement().ToString() + " "+returnValue ;
firstElement = firstElement.GetEClass(); ;
} while (firstElement.GetEClass() != null); return returnValue ;
}
}
此程序我不懂的是它为什么用ADDE能加像集合类一样加入元素,用firstElement。GetEClass能取得下一个元素,我怎么看GElementList<T>也都只是一个普通类,而不像是一个集合类,请大家帮我解释一下
{
static void Main(string[] args)
{
GElementList<int> theGElementList = new GElementList<int>() ;
for (int i = 1; i < 6; i++ )
theGElementList.AddE(i*10); string listValue = theGElementList.GetListData();
Console.WriteLine(listValue); GElementList<string> theGElementList1 = new GElementList<string>(); theGElementList1.AddE("A");
theGElementList1.AddE("B");
theGElementList1.AddE("C");
theGElementList1.AddE("D");
theGElementList1.AddE("E");
listValue = theGElementList1.GetListData();
Console.WriteLine(listValue);
Console.Read();
}
}
public class ElementS<T>
{
private T cElement ;
public T GetElement()
{
return cElement;
}
public void SetElement(T pElement)
{
this.cElement = pElement;
}
private ElementS<T> theElementS = null ; public ElementS<T> GetEClass()
{
return theElementS;
}
public void SetEClass(ElementS<T> pElementS)
{
this.theElementS = pElementS;
}
}
public class GElementList<T>
{
private ElementS<T> firstElement = new ElementS<T>() ;
public void AddE(T pElement)
{
ElementS<T> newElementS = new ElementS<T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
public string GetListData()
{
string returnValue = "" ;
do
{
returnValue = firstElement.GetElement().ToString() + " "+returnValue ;
firstElement = firstElement.GetEClass(); ;
} while (firstElement.GetEClass() != null); return returnValue ;
}
}
此程序我不懂的是它为什么用ADDE能加像集合类一样加入元素,用firstElement。GetEClass能取得下一个元素,我怎么看GElementList<T>也都只是一个普通类,而不像是一个集合类,请大家帮我解释一下
{
ElementS <T> newElementS = new ElementS <T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
知之为知之,不知google之
在这里GElementList <T>被实例化为int类型的,即T是一个泛型类,可以被实例化为任意的类型。
{
ElementS <T> newElementS = new ElementS <T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
看到上面这行代码了没?
就是通过它把链表中的节点加入链表,实现链接~c#里面没有指针,取而代之相同作用的是引用,本质上就是指针