怎么在一个类的方法中调用另一个类的方法和属性啊? ( 积分: 0 )

  • 主题发起人 主题发起人 bota
  • 开始时间 开始时间
B

bota

Unregistered / Unconfirmed
GUEST, unregistred user!
unit GisMap;
interface
uses
SysUtils, Classes, Controls, OleCtrls, MapXLib_TLB;
type
TGisMap = class(TMap)
private
FMapTitle :string;
protected
procedure SetMapTitle(Value :string);
public
property MapTitle : string read FMapTitle write SetMapTitle;
published
end;

TCar = class
private
public
procedure test();
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('ActiveX', [TGisMap]);
end;

procedure TGisMap.SetMapTitle(Value :string);
begin
FMapTitle :=Value;
end;

procedure TCar.test();
begin
TGisMap.MapTitle :='ok';//这里应该怎么调用?
end;

end.
 
unit GisMap;
interface
uses
SysUtils, Classes, Controls, OleCtrls, MapXLib_TLB;
type
TGisMap = class(TMap)
private
FMapTitle :string;
protected
procedure SetMapTitle(Value :string);
public
property MapTitle : string read FMapTitle write SetMapTitle;
published
end;

TCar = class
private
public
procedure test();
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('ActiveX', [TGisMap]);
end;

procedure TGisMap.SetMapTitle(Value :string);
begin
FMapTitle :=Value;
end;

procedure TCar.test();
begin
TGisMap.MapTitle :='ok';//这里应该怎么调用?
end;

end.
 
unit Unit1;

interface

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

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

TCar = class
private
FControl: TControl;
procedure SetControl(AControl: TControl);
public
property Control: TControl read FControl write SetControl;
procedure Test;
end;

TGisMap = class(TControl)
private
FMapTitle :string;
FCar: TCar;
protected
procedure SetMapTitle(Value :string);
public
constructor Create(AOwner: TComponent); override;
property MapTitle : string read FMapTitle write SetMapTitle;
published
end;


var
Form1: TForm1;
aGisMap: TGisMap;

procedure Register;

implementation

{$R *.dfm}

procedure Register;
begin
RegisterComponents('ActiveX', [TGisMap]);
end;

constructor TGisMap.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCar := TCar.Create;
FCar.Control := Self;
end;

procedure TGisMap.SetMapTitle(Value :string);
begin
FMapTitle := Value;
end;

procedure TCar.SetControl(AControl: TControl);
begin
if FControl <> AControl then FControl := AControl;
end;


procedure TCar.test;
begin
showmessage(TGisMap(FControl).MapTitle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
aGisMap := TGisMap.Create(Self);
end;

end.


请不要单独声明TCar变量
 
记住你不能直接调用类的属性和方法,而应该调用类的实例的属性和方法。(除非你调用的方法是声明成类方法的)
 
可否通过parent来实现呢
 
我建议你在类的架构上下点功夫,引入接口机制能很好解决你的问题
 
谢谢大家
 
后退
顶部