我看到一个泛型类中又包含了一个类,没见过这种用法!
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{
protected Node head;
protected Node current = null; // Nested class is also generic on T
protected class Node
{
public Node next;
private T data; //T as private member datatype public Node(T t) //T used in non-generic constructor
{
next = null;
data = t;
} public Node Next
{
get { return next; }
set { next = value; }
} public T Data //T as return type of property
{
get { return data; }
set { data = value; }
}
} public GenericList() //constructor
{
head = null;
} public void AddHead(T t) //T as method parameter type
{
Node n = new Node(t);
n.Next = head;
head = n;
} // Implementation of the iterator
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
} System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}看不明白什么意思!
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{
protected Node head;
protected Node current = null; // Nested class is also generic on T
protected class Node
{
public Node next;
private T data; //T as private member datatype public Node(T t) //T used in non-generic constructor
{
next = null;
data = t;
} public Node Next
{
get { return next; }
set { next = value; }
} public T Data //T as return type of property
{
get { return data; }
set { data = value; }
}
} public GenericList() //constructor
{
head = null;
} public void AddHead(T t) //T as method parameter type
{
Node n = new Node(t);
n.Next = head;
head = n;
} // Implementation of the iterator
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
} System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}看不明白什么意思!
为了加强Node这个类,现在把类型改成了泛型T,并实现了IEnumerable接口(支持for each遍历)
protected class Node
{
public Node next;
private T data;刚声明了个类Node
但是下面又声明了public Node next;
可以这样用?