本人在看programming C#(中文版) 电工出版 上面看到这样一行.
public class Node<T>:IComparable<Node<T>> where T:IComparable<T>{ private T data; ...}
书上是这样描述的:实现一个简化的可排序单链表.表是由多个结点组成,没个结点必须保证添加到其上的类型实现IComparer.
我想问的就是.这一行代码.如果写成这样的话.会有什么不同呢?
public class Node<T>:IComparable<T> where T:IComparable<T>{private T data; ...}
还有就是他里面有一个私有变量data..T暂时是不不知道的类型.如果传进去的是引用类型也能这样直接用"private"定义吗?
为了大家看得更清楚.我还是把Node<T>的全部代码都弄上来.
public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
{
private T data;
private Node<T> next = null;
private Node<T> prev = null; public Node(T data)
{
this.data = data;
} public T Data{get {return this.data}} public Node<T> Next
{
get { return this.next; }
} public int CompareTo(Node<T> rhs)
{
return data.CompareTo(rhs.data);
} public bool Equals(Node<T> rhs)
{
return this.data.Equals(rhs.data);
} public Node<T> Add(Node<T> newNode)
{
if (this.CompareTo(newNode) > 0)
{
newNode.next = this;
if (this.prev != null)
{
this.prev.next = newNode;
newNode.prev = this.prev;
}
this.prev = newNode;
return newNode;
}
else
{
if (this.next != null)
{
this.next.Add(newNode);
}
else
{
this.next = newNode;
newNode.prev = this;
}
return this;
}
} public override string ToString()
{
string output = data.ToString();
if (next != null)
{
output += ", " + next.ToString;
}
return output;
}
}
public class Node<T>:IComparable<Node<T>> where T:IComparable<T>{ private T data; ...}
书上是这样描述的:实现一个简化的可排序单链表.表是由多个结点组成,没个结点必须保证添加到其上的类型实现IComparer.
我想问的就是.这一行代码.如果写成这样的话.会有什么不同呢?
public class Node<T>:IComparable<T> where T:IComparable<T>{private T data; ...}
还有就是他里面有一个私有变量data..T暂时是不不知道的类型.如果传进去的是引用类型也能这样直接用"private"定义吗?
为了大家看得更清楚.我还是把Node<T>的全部代码都弄上来.
public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
{
private T data;
private Node<T> next = null;
private Node<T> prev = null; public Node(T data)
{
this.data = data;
} public T Data{get {return this.data}} public Node<T> Next
{
get { return this.next; }
} public int CompareTo(Node<T> rhs)
{
return data.CompareTo(rhs.data);
} public bool Equals(Node<T> rhs)
{
return this.data.Equals(rhs.data);
} public Node<T> Add(Node<T> newNode)
{
if (this.CompareTo(newNode) > 0)
{
newNode.next = this;
if (this.prev != null)
{
this.prev.next = newNode;
newNode.prev = this.prev;
}
this.prev = newNode;
return newNode;
}
else
{
if (this.next != null)
{
this.next.Add(newNode);
}
else
{
this.next = newNode;
newNode.prev = this;
}
return this;
}
} public override string ToString()
{
string output = data.ToString();
if (next != null)
{
output += ", " + next.ToString;
}
return output;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货