类方法中跟类类型相关,如何实现多态?(50分)

  • 主题发起人 主题发起人 Adnil
  • 开始时间 开始时间
A

Adnil

Unregistered / Unconfirmed
GUEST, unregistred user!
困扰已久。

TForm1 = class(TADWizardDialog)
public
class function Execute(AOwner: TComponent): integer
virtual;
end;

class function TForm1.Execute(AOwner: TComponent): integer;
var
Form: TForm1;
begin
Form := TForm1.Create(AOwner)
//这里如何调用TForm1的子类?
with Form do
try
ShowModal;
finally
Release;
end;
end;

TForm2 = class(TForm1)
Form2:TForm2;
Form2.Execute(Self)执行的时候显示的是Form2,而不是Form1,有办法吗?
 
Form2.Execute(Self),這兒本身就是指定的Form2﹐當然顯示Form2﹐你改成
Form2.Execute(Form1)
 
兄弟,看看差不多的问题吧
http://www.delphibbs.com/delphibbs/dispq.asp?lid=726479
 
databox
你看懂我的问题了吗?AOwner是控件所有者,跟控件的类方法没有关系。
 
此代码显示的就是form1啊!怎么有问题?
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
class function Execute(AOwner: TComponent): integer
virtual;
{ Public declarations }
end;
TForm2 = class(TForm1);
var
Form1: TForm1;

implementation

{$R *.DFM}




class function TForm1.Execute(AOwner: TComponent): integer;
var
Form: TForm1;
begin
Form := TForm1.Create(AOwner)
//这里如何调用TForm1的子类?
with Form do
try
ShowModal;
finally
Release;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Form2:TForm2;
begin
Form2:= TForm2.Create(self);
Form2.Execute(Self)
form2.free;
end;

end.
 
var
Form2:TForm2;
begin
Form2:= TForm2.Create(self);
Form2.Execute(Self)
//这是的调用只是显示出了一个Form1,而我需要显示Form2
form2.free;
而且Excute是类方法,不需要创建类的实例
例如我声明
TForm2 = class(TForm1)
Edit2: TEdit;
end;
那么TForm2.Execute的时候应该同时显示Edit2和其父类的Edit1,Who can do it?
 
建议回答本问题的朋友需要先清楚多态,继承,类方法,抽象等OO概念。
 
这是谁写的!!!!!!看你的第一个贴子!
tmd
Form2.Execute(Self)执行的时候显示的是Form2,而不是Form1,有办法吗?

用类参考
tform1class=class of tform1
 
//真受不了你!!!,看下面
unit Unit1;

interface

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

type
Form1Class= class of TForm1;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
class function Execute(formtype:form1class;AOwner: TComponent): integer
virtual;
{ Public declarations }
end;

TForm2 = class(TForm1)
public
constructor Create(AOwner: TComponent)
override;
end;
var
Form1: TForm1;

implementation

{$R *.DFM}



//产生什么类型的窗体,就把他的类名传进去.
class function TForm1.Execute(formtype:form1class;AOwner: TComponent): integer;
var
Form: TForm1;
begin
Form := formtype.Create(AOwner)
//这里如何调用TForm1的子类?
with Form do
try
ShowModal;
finally
Release;
end;
end;

//显示form2
procedure TForm1.Button1Click(Sender: TObject);
var
Form2:TForm2;
begin
Form2:= TForm2.Create(self);
Form2.Execute(tform2,Self)
form2.free;
end;

{ TForm2 }

constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
caption:='form2';
end;

//显示form1
procedure TForm1.Button2Click(Sender: TObject);
var
Form2:TForm2;
begin
Form2:= TForm2.Create(self);
Form2.Execute(tform1,Self)
form2.free;
end;

end.
 
方法是可行的,传类名,但是这样就没有办法约束了,没有多态的成分。
想想,类方法也是类的组成部分,自己的类型是否可以自己获得,而不需
要调用者通过参数来控制。

前面的可能是我没有说清楚,见谅!

顺便纠正你代码中的一个不足:
procedure TForm1.Button1Click(Sender: TObject);
var
Form2:TForm2;
begin
Form2:= TForm2.Create(self);
Form2.Execute(tform2,Self)
form2.free;
end;
省略成
procedure TForm1.Button1Click(Sender: TObject);
begin
TForm2.Execute(tform2,Self)
end;
即可,类方法不需要生成实例,当然如果类方法中要创建对象,那得在
类方法中同时释放创建的对象。
 
这方面我也不是非常清楚,所以才想发贴子探讨的。
现在完成一个单元如下:
unit Unit1;

interface

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

type

TFormClass = class of TForm;

TMyForm = class(TForm)
public
class function Execute(formtype:TFormClass
AOwner: TComponent): integer
virtual;
constructor Create(AOwner: TComponent)
override;
end;

TMyForm1 = class(TMyForm)
constructor Create(AOwner: TComponent)
override;
end;

TMyForm2 = class(TMyForm)
constructor Create(AOwner: TComponent)
override;
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{ TMyForm }
constructor TMyForm.Create(AOwner: TComponent);
begin
inherited;
Width := 200;
Height := 200;
Position := poMainFormCenter;
end;

class function TMyForm.Execute(formtype:TFormClass
AOwner: TComponent): integer;
begin
with formtype.CreateNew(AOwner) do
try
Result := ShowModal;
finally
Release;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TMyForm1.Execute(TMyForm1, Self);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TMyForm2.Execute(TMyForm2, Self);
end;

{ TMyForm1 }

constructor TMyForm1.Create(AOwner: TComponent);
begin
inherited;
Caption := 'This is TMyForm1';
end;

{ TMyForm2 }

constructor TMyForm2.Create(AOwner: TComponent);
begin
inherited;
Caption := 'This is TMyForm2';
end;

end.

尚存在两个问题:
1.TMyForm1.Execute(TMyForm1, Self);实际还只是生成了TMyForm对象
2.如何能够优化成
TMyForm1.Execute(Self);执行TMyForm1
TMyForm2.Execute(Self);执行TMyForm2

盼望OO高手来看看。 :)
 
procedure TForm1.Button1Click(Sender: TObject);
begin
TForm2.Execute(tform2,Self)
end;
这句代码确实比我的好
但是你也可以验证,类方法是不与对象相关的,我不知道在没有对象的情况下
,有没有可能知晓这个类的一些信息,我觉得可能性不大(没有rtti啊)
 
呵呵,我看看!self.create...有点古怪。。。
 
我们都是笨蛋,哈哈,交个朋友吧
 
we are fool
直接用居然都可以,不知道这样是不是你想要的
Thus you cannot useSelf to access fields, properties, and normal (object) methods,
but you can use it to call constructors and other class methods.
A class method can be called through a class reference or an object reference. When it is called through an object reference,
the class of the object becomes the value of Self.

type
form1class= class of TForm1;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
class function Execute(formtype:form1class;AOwner: TComponent): integer
virtual;
{ Public declarations }
end;

TForm2 = class(TForm1)
public
constructor Create(AOwner: TComponent)
override;
end;
var
Form1: TForm1;

implementation

{$R *.DFM}
class function TForm1.Execute(AOwner: TComponent): integer;
var
//基类
Form: TForm1;
begin
showmessage(self.ClassName);
Form := self.Create(AOwner);
with Form do
try
ShowModal;
finally
Release;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TForm2.Execute(Self)
end;

{ TForm2 }

constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
caption:='form2';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TForm1.Execute(Self)
end;

end.
 
哈哈,不错不错,调试通过了,没有必要指定类型,Self.构造函数即可。嗬嗬!
交个朋友,我oicq 115261
 
好象不在线?
我在北京用qq也不方便,还是回武汉再用qq和你聊吧,呵呵
zj1978@163.net
 
呵呵,只是我隐身了 :)

给分!
 
多人接受答案了。
 
后退
顶部