C
corbaejb
Unregistered / Unconfirmed
GUEST, unregistred user!
在delphi中对象的创建是否一定要用create动态创建!我自己创建了一个类,调用如下,运行
时出现存取错误:
unit Unit2;
interface
type
TMyClass = class
private
{ Private declarations }
public
{ Public declarations }
x :integer;
y :integer;
z :integer;
s :String;
constructor create;
procedure Add;
end;
implementation
constructor TMyClass.create;
begin
x:=0;
y:=0;
z:=0;
s:='';
end;
procedure TMyClass.Add;
begin
z:=x+y;
s:='success';
end;
end.
////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
testobj :TMyClass;
begin
testobj.x:=10;
testobj.y:=20;
testobj.Add;
ShowMessage(inttostr(testobj.z));
Showmessage(testobj.s);
end;
end.
时出现存取错误:
unit Unit2;
interface
type
TMyClass = class
private
{ Private declarations }
public
{ Public declarations }
x :integer;
y :integer;
z :integer;
s :String;
constructor create;
procedure Add;
end;
implementation
constructor TMyClass.create;
begin
x:=0;
y:=0;
z:=0;
s:='';
end;
procedure TMyClass.Add;
begin
z:=x+y;
s:='success';
end;
end.
////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
testobj :TMyClass;
begin
testobj.x:=10;
testobj.y:=20;
testobj.Add;
ShowMessage(inttostr(testobj.z));
Showmessage(testobj.s);
end;
end.