关于对象的奇怪问题,请高手看一下,多谢了(200分)

  • 主题发起人 主题发起人 xiaoyin
  • 开始时间 开始时间
X

xiaoyin

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟初学delphi,遇到了两个问题,
1 :对象没有创建,对象方就可以使用;
2 :解除对象后,方法仍然可以使用;
代码如下:
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TBase = Class
Function Add(x,y:integer):integer;
end;
TBaseOne = Class(TBase)
Function Sub(X,y:integer): integer;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TBaseOne }

function TBaseOne.Sub(X,y:integer): integer;
begin
Result := x - y;
end;

{ TBase }

function TBase.Add(x, y: integer): integer;
begin
Result := x + Y;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s : string;
BaseOne : TBaseOne

begin
BaseOne := TBaseOne.Create;
try
s := Format('The Sub Result is %D',[BaseOne.sub(3,7)]);
ShowMessage(s);
Finally
BaseOne.Free;
BaseOne := nil;
end;
s := Format('The Sub Result is %D',[BaseOne.sub(7,4)]);//返回'4'
ShowMessage(s);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
BaseOne : TBaseOne;
s : String;
begin
S := Format('The Sub Result is %D',[BaseOne.sub(7,4)]);
ShowMessage(s);

end;
两个按钮过程都可以通过,返回了结果,是怎么回事?
 
不是没有创建,是DELPHI自动创建的,你FREE之后再调用也是DELPHI自动创建的
创建分显示创建和隐式创建两种
 
因为你在类里没有内存操作,所以不用创建也可以用。
 
能具体些么.什么时候是隐式创建呢;谢谢
 
动了一下:
TBaseOne = Class(TBase)
private
x ,y : integer;
function Sub(X, y: integer): integer;
public
Function GetSub: integer;
procedure SetValue(X1,y1:integer);
end;
在这个过程还是可以使用;
procedure TForm1.Button2Click(Sender: TObject);
var
BaseOne : TBaseOne;
s : String;
begin
BaseOne.SetValue(10,20);
S := Format('The Sub Result is %D',[BaseOne.Getsub]);
ShowMessage(s);

end;
怎么回事
 
动了一下:
TBaseOne = Class(TBase)
private
x ,y : integer;
function Sub(X, y: integer): integer;
public
Function GetSub: integer;//返回 X + Y
procedure SetValue(X1,y1:integer);//赋值;X := X1,Y = Y1;
end;
在这个过程还是可以使用;
procedure TForm1.Button2Click(Sender: TObject);
var
BaseOne : TBaseOne;
s : String;
begin
BaseOne.SetValue(10,20);
S := Format('The Sub Result is %D',[BaseOne.Getsub]);
ShowMessage(s);

end;
怎么回事
 
http://www.delphibbs.com/delphibbs/dispq.asp?LID=1294954
http://www.delphibbs.com/delphibbs/dispq.asp?LID=1047021
 
这样基于TObject而没有任何成员变量和属性的类,只是方法,是可以不要创建的,
这种用法并不值得推荐,因为这和定义一些全局函数没有任何区别,
并没有体现出类和封装的特性,一旦你使用了成员变量和属性,你的代码将出现错误。
请看下面的代码:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TBase = Class
FValue:Integer;
Function Add(x,y:integer):integer;
end;
TBaseOne = Class(TBase)
Function Sub(X,y:integer): integer;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
{ TBaseOne }

function TBaseOne.Sub(X,y:integer): integer;
begin
FValue:=0;
Result := x - y+FValue;
end;

{ TBase }

function TBase.Add(x, y: integer): integer;
begin
FValue:=0;
Result := x + Y+FValue;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
s : string;
BaseOne : TBaseOne

begin
BaseOne := TBaseOne.Create;
try
s := Format('The Sub Result is %D',[BaseOne.sub(3,7)]);
ShowMessage(s);
Finally
BaseOne.Free;
BaseOne := nil;
end;
s := Format('The Sub Result is %D',[BaseOne.sub(7,4)]);//返回'4'
ShowMessage(s);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
BaseOne : TBaseOne;
s : String;
begin
S := Format('The Sub Result is %D',[BaseOne.sub(7,4)]);
ShowMessage(s);

end;

end.


改管以上的代码运行时还能得出正确的结果,但是当你关闭程序的时候就会出错。
 
to:xiaoyin
去 http://www.delphibbs.com/delphibbs/dispq.asp?LID=1294954
听听 creation-zy,dirk,SS2000等几位大侠的讲课。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
457
import
I
I
回复
0
查看
759
import
I
I
回复
0
查看
496
import
I
后退
顶部