Element:array[1..StackSize] of char;
procedure Push(Item:char);
procedure Pop(var Item:char);
function IsEmpty:boolean;
function IsFull:boolean;
private
Top:0..StackSize;
constructor Create;
destructor Destroy;override;
public
end;
妳這個類寫的就有問題, constructor Create;
destructor Destroy;override;
這兩個方法應該是Public的。 如果妳想要在類以外使用這個東東,Top:0..StackSize;,應該把它定義成property,妳這樣定義的東西,在這個文件以外的地方是沒法使用的,也不合面象對象編程的思想。
code.Element[Top]妳這句裏面的Top引用的並不是TStack類裏面的Top,由於妳沒有定義Top變量,這個Top其實是妳的窗口的Top,也就是Form1.Top.
我幫妳把這個類改了壹下,參考壹下吧:
const
StackSize = 3;
type
TStackSize = 0..StackSize-1;
TStack=class
private
FTop: Integer;
Element:array[0..StackSize-1] of char;
public
constructor Create;
destructor Destroy;override;
procedure Push(Item:char);
procedure Pop(var Item:char);
function IsEmpty:boolean;
function IsFull:boolean;
published
property Top: Integer read FTop;
end;
constructor TStack.Create;
begin
FTop := -1;
end;
destructor TStack.Destroy;
begin
inherited;
end;
function TStack.IsEmpty: boolean;
begin
result := FTop = -1;
end;
function TStack.IsFull: boolean;
begin
result := FTop = StackSize - 1;
end;
procedure TStack.Pop(var Item: char);
begin
if FTop < 0 then exit;
Item := Element[FTop];
Dec(FTop);
end;
procedure TStack.Push(Item: char);
begin
if FTop >= StackSize then exit;
Element[FTop] := Item;
Inc(FTop);
end;
這個類是按照妳原來的意思定義的,真正使用起來的話還有很多問題的。象棧的大小就不能這樣定義。