unit MyList;interfacetype
  Node=class
    no:integer;
    name:string;
end;type
  NodeList=class
  private
    nodes:array[1..100] of Node;
  public
    Length,Current:integer;
    constructor Create;
    procedure Add(nd:Node);
    function  FindNo(Name:string):integer;
    procedure Clear();
    function  Next:Node;
    function  First:Node;
  end;implementation{ NodeList }procedure NodeList.Add(nd: Node);
begin
  inc(Length);
  Current:=Length;
  Nodes[Length]:=nd;
end;procedure NodeList.Clear;
begin  Current:=0;
  Length:=0
end;constructor NodeList.Create;
begin
  Length:=0;
  Current:=0;
end;function NodeList.FindNo(Name: string): integer;
var
  i:integer;
begin
  result:=-1;
  for i:=1 to Length do
  begin
    if(Nodes[i].name=Name) then
    begin
      result:=Nodes[i].no;
      exit;
    end;
  end;
end;
function NodeList.First: Node;
begin
  if(Length>0) then
    result:=Nodes[1];
end;function NodeList.Next: Node;
var
  nd:Node;
begin
  if(Length=0) then
  begin
    nd.no:=-1;
    nd.name:='';
    Result:=nd;
  end;
  if(Length>Current) then
  begin
    inc(Current);
    result:=Nodes[Current];
  end
  else if(Length=Current) then
  begin
    result:=Nodes[Current];
  end;
end;end.
----------
这是我编的一个类,数组列表类,但是不知道怎么使用它,在C++里面有new,那么在pascal怎么用呢?

解决方案 »

  1.   

    晕,楼主你不是在开玩笑吧,类写出来了都不知道怎么用?声明一个N:NodeList变量,然后N:=NodeList.Create;,再调用它的各种方法啊。
      

  2.   

    ^_^
     
    最后还要 用free 释放
      

  3.   

    楼主,Delphi中有个约定,类都以T打头,所以把你的类改名为TNodeList看起来更顺眼一些:)
      

  4.   

    delphi 里的对象 都在堆上创建,而且,必须显示的 调用 类的 构造函数,
    不像C++中,编译器可以自动调默认构造函数。而且,因为是在堆上,所以记得用完
    调用Free方法释放内存