这是阿尔巴哈里(Albarhari)的例子
public class Stack
{
int position;
object[] data = new object[10]; // Why 10 nor 1?
public void Push (object obj) { data[position++] = obj; } //do not understood; Why there is no loop
public object Pop() { return data[--position]; } //do not understood Why there is no loop
}
Stack stack = new Stack();
stack.Push ("sausage");
string s = (string) stack.Pop(); // Downcast, so explicit cast is needed
Console.WriteLine (s); // sausage
我重写了这里听过的代码
public class Stack
{
object[] data = new object[1];
public void Push(object obj) { data[0] = obj; }
public object Pop() { return data[0]; }
}
Stack stack = new Stack();
stack.Push("abigale ff");
string s = (string)stack.Pop();
Console.WriteLine(s); // abigale ff
为什么用10 in new object[10];而不是1或100 in为何在数据位置使用增量?我不了解数据位置的工作原理。
{ data[position++] = obj; }以及 { return data[--position]; }它如何工作而没有循环?我尝试在弹出之前推送2个值,然后在弹出之前写入它,但它仅显示第二个值
MMMHUHU
相关分类