我明白了,你是把类当成对象了。对象是类的实例啊,对象1的值怎么能赋给类呢?类只是一
个抽象,对象才执行实际动作。看看这个:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Label1: TLabel;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TA = class
private
s1: string;
procedure setStr(str: string);
published
property s: string read s1 write setStr;
end;
var
Form2: TForm2;
c1: TA;
implementation
{$R *.dfm}
procedure TA.setStr(str: string);
begin
s1 := str;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
label1.Caption := c1.s;
end;
end.
////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Edit1: TEdit;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
c1 := TA.Create;
c1.s := Edit1.Text;
ShowMessage(c1.s);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form2.Show;
end;
end.