菜鸟级问题:请问如何重载一个属性? --by 萧月禾(300分)

  • 主题发起人 萧月禾
  • 开始时间
祝萧工圣诞快乐 ^_^
 
萧月禾:
您的这道题可象是绕口令一样,两个FORM是继承,BASE,BASE1又继承,头都被你搞大了!
其实很简单,跟BASE BASE是否被继承没有关系的,你是想让TFORM2有一个与TFORM1相同名称的属性,只要在TFORM2中按正常的方法重新定义一个TBASE1(或是其他类型如Base:Integer,都是可以的),这样TForm1中的属性Base就只能在Form1中使用,而TFrom2中的Base就能在Form2中发挥正常的功能了,你要的是这样吗?下面是代码"
==================================================================
unit Unit1;

interface

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

type
TBase = class(TObject)
public
procedure s;
end;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Fbase: Tbase;
{ Private declarations }
public
{ Public declarations }
published
property base: Tbase read Fbase write Fbase;
end;



var
Form1: TForm1;

implementation

procedure TBase.s;
begin
showmessage('base');
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
base.s;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FBase:=TBase.create;
end;

end.
=========================================================================
unit Unit2;

interface

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

type
TForm2 = class(TForm1)
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FBase: TBase;
function GetBase: TBase;
{ Private declarations }
public
{ Public declarations }
published
property Base read FBase write FBase;
end;

TBase1 = class(tbase)
public
procedure s;
end;

var
Form2: TForm2;

implementation

procedure TBase1.s;
begin
showmessage('base1');
end;

{$R *.dfm}

procedure TForm2.Button2Click(Sender: TObject);
begin
inherited;
TBase1(base).s;
end;

function TForm2.GetBase: TBase;
begin
if not Assigned(FBase) then
FBase:=TBase1.Create;
Result := FBase;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
inherited;
FBase:=TBase1.create;
end;

end.
=========================================
其实是ModalMake Code Explorer告诉我的:>)
 
我就是不想用强制类型转换的方式来做呀,否则就不叫问题了
 
好像没有必要。你的Base对象都是在form自己创建的,form2内直接通过对象访问。以属性的方式public出去,是为了外界访问。
 
不用强制类型转换好象是不行,也许是我水平大次,不过如果是嫌每次使用Base都要转换麻
烦,看这样行不行:
Unit1.pas

TBase = class
public
abc: string;
procedure Show1;
end;

TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);//这里调用Base的show1
procedure FormCreate(Sender: TObject);//这里创建Base对象
private
function GetBase:TBase;
public
property Base: TBase read GetBase;... //定义TBase的对象变量
end
^^^^^^^^


Unit2.pas

TBase1 = class(TBase) //从Unit1的
public
procedure Show2;
end;

TForm2 = class(TForm1)
procedure Button2Click(Sender: TObject);//这里调用Base的show1
procedure FormCreate(Sender: TObject);//这里创建Base对象,不用inherited
private
function GetBase1:TBase1;
public
property Base: TBase1 read GetBase1;... //重新用TBase1定义Base对象变量
end;

然后TForm1中的Base的类型为TBase 面TForm2中的Base的类型就为TBase1了,不过在TForm2
中就必须实例化一个TBase1类型的对象.
 
以上所有方式都不成功,是我太笨了。
最后还是用了强制类型转换的方式调用,不重新在TForm2中定义Base
而是只是用TBase1去创建它。。。。
程序用起来倒是正常,只是感觉这种方式不太好

明天上午9:30要给客户用了,来不及慢慢研究。

顺便发一下牢骚,TMD,今天被一个可恶的女人气惨了
既愤怒又无奈,砸东西发泄,弄坏了电脑桌的键盘托板
还把手弄伤了(连续用拳头打墙弄的)
2003年的平安夜,生气中度过。。。
 
萧:
你这个题目中的例子在设计上就存在着逻辑错误,
所以就无法实现。

你不要用你作好的有逻辑问题的例子让大家改,
这样再改也是错误的,你说一下你想实现什么样的功能,
再由大家帮你设计一个好的方式去作。
 
大家圣诞快乐。。。。。。。。睡觉去。。。。。[:D]
 
TForm2 = class(TForm1)
// procedure Button2Click(Sender: TObject);//这里调用Base的show1
// procedure FormCreate(Sender: TObject);//这里创建Base对象,不用inherited
function GetBase:TBase1;
public
// property Base: TBase1 ... //重新用TBase1定义Base对象变量
property Base:TBase1 read GetBase;
end;
.....
function TForm2.GetBase:TBase1;
begin
Result:=Inherited Base as TBase1
end;

 
Unit1.pas

TBase = class
public
abc: string;
procedure Show1;
end;

TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
BaseObj: TBaseObj;
public
property Base: TBase read BaseObj write BaseObj;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Base := TBase.Create;
end;

Unit2.pas

TBase1 = class(TBase)
public
procedure Show2;
end;

TForm2 = class(TForm1)
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
function GetBase: TBase1;
procedure SetBase(Base: TBase1);
public
property Base: TBase1 read BaseObj write BaseObj;
end;

function TForm2.GetBase: TBase1;
begin
Result := TBase1(inherited Base);
end;

procedure TForm2.SetBase(Base: TBase1);
begin
inherited Base := Base;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
Base.Free;
Base := TBase1.Create;
end;
 
不知萧月禾,解决了没有
 
看看VCL的例子
TList = class(TObject)
private
FList: PPointerList;
FCount: Integer;
FCapacity: Integer;
protected
function Get(Index: Integer): Pointer;
procedure Put(Index: Integer
Item: Pointer);
...
public
.....
property Items[Index: Integer]: Pointer read Get write Put
default;
end;
TObjectList = class(TList)
private
FOwnsObjects: Boolean;
....
protected
function GetItem(Index: Integer): TObject;
procedure SetItem(Index: Integer
AObject: TObject);
...
public
constructor Create
overload;
constructor Create(AOwnsObjects: Boolean)
overload;
....
property Items[Index: Integer]: TObject read GetItem write SetItem
default;
end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
610
import
I
I
回复
0
查看
626
import
I
顶部 底部