我定义一个类,给这个类传入参数,并让这个类的公共变量sr改变
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IniFiles, ComCtrls, Buttons;type
  test = class
    sr: string;
    constructor create();overload;
    constructor create(s: string); overload;
    procedure zvoid;
  end;implementationconstructor create(s: string);//错误??
begin
  inherited create;  sr := s;  //sr错误 了
end;
procedure zvoid;
begin
  ShowMessage(sr);
end;end.如果我在别一个类中调用的话,应是这样的procedure testMouse.FormCreate(Sender: TObject);
var
 ts:test;
 s:string;
begin
  ts:=test.create(s);//错误 了?参数不正确
  ts.zvoid;
end;

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TTest = class
      private
        str:string;    procedure zvoid;
      public
        constructor Create(s:string);
      end;
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TTest }constructor TTest.Create(s: string);
    begin
      str := s;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      test : TTest;
    begin
      test := TTest.Create('abc');
      test.zvoid;
    end;procedure TTest.zvoid;
    begin
      ShowMessage(str);
    end;end.