delphi 成员变量赋值异常
我写了一个很简单的类,但用类建立对象后给i赋值总是出现异常,请问是怎么回事,麻烦简答一下,谢谢!!!unitUnit1;interfaceusesWindows,Mess...
我写了一个很简单的类,但用类建立对象后给i赋值总是出现异常,请问是怎么回事,麻烦简答一下,谢谢!!!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyClass = class(Tobject)
private
i:integer;
public
procedure SetNum(a:Integer);
function GetNum():Integer;
end;
var
Form1: TForm1;
ob:TMyClass;
implementation
{$R *.dfm}
procedure TMyClass.SetNum(a:Integer);
begin
i:=a;
end;
function TmyClass.GetNum():Integer;
begin
result:=i;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ob.SetNum(2);
Label1.Caption:=inttostr(ob.GetNum());
end;
end. 展开
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyClass = class(Tobject)
private
i:integer;
public
procedure SetNum(a:Integer);
function GetNum():Integer;
end;
var
Form1: TForm1;
ob:TMyClass;
implementation
{$R *.dfm}
procedure TMyClass.SetNum(a:Integer);
begin
i:=a;
end;
function TmyClass.GetNum():Integer;
begin
result:=i;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ob.SetNum(2);
Label1.Caption:=inttostr(ob.GetNum());
end;
end. 展开
1个回答
展开全部
你的ConStructor那裏去了?没有创建的函数阿。
类必须要重新覆盖创建的函数。
例如:
Constructor Create;override;
procedure TMyClass.Create;
begin
inherride Create(AWoner);
end;
在使用的时候要先实例化这个类才可以使用,(静态类不用创建)
var
my:TMyclass;
begin
my := TMyclass.Create;
my.SetNum;
my.GetNum;
end;
类必须要重新覆盖创建的函数。
例如:
Constructor Create;override;
procedure TMyClass.Create;
begin
inherride Create(AWoner);
end;
在使用的时候要先实例化这个类才可以使用,(静态类不用创建)
var
my:TMyclass;
begin
my := TMyclass.Create;
my.SetNum;
my.GetNum;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询