遇到这样一个类指针赋值的问题,哪位大哥遇到过帮小弟解释一下!(200分)

  • 主题发起人 主题发起人 我就是一个问题
  • 开始时间 开始时间

我就是一个问题

Unregistered / Unconfirmed
GUEST, unregistred user!
Var
insertflag, eraseflag: integer;
point:PTIOPOINT;
Begin
eraseflag := 1;
insertflag := 1;
new(point);
Case (pID) Of //根据元件的不同来选择画什么元件
1: Begin
point^:=LD.Create;
point^.setflag(1);
point^.setcode('LD');
End;
2: Begin
point^ := LDI.Create;
point^.setflag(2);
point^.setcode('LDI');
End;

Type
LD = Class(TIOPOINT)
Private
{ Private declarations }
Public
{ Public declarations }
Procedure IOPOINT(x, y, flg: integer; cd, nm, ad: String); Overload;
Procedure IOPOINT(); Overload;
Procedure put_img(x, y: integer; MCanvas: TCanvas);
Procedure print(x, y: integer; MCanvas: TCanvas);
End;

Type
PTIOPOINT = ^TIOPOINT;
TIOPOINT = Class(TObject)
Private
{ Private declarations }
Protected
name: String;
code: String;
addr: String;
xpoint, ypoint: integer;
flag: integer;
fiveflag: integer;
visit: Boolean;
right: PTIOPOINT;
down: PTIOPOINT;
left: PTIOPOINT;
up: PTIOPOINT;
unup: PTIOPOINT;
undown: PTIOPOINT;

Public
{ Public declarations }
Procedure IOPOINT(x, y, fl: integer; cd, nm, ad: String); Overload;
Procedure IOPOINT(); Overload;
Procedure setname(ioname: String);
Procedure setaddr(ioaddr: String);
Procedure setcode(iocode: String);
Procedure setflag(flg: integer);
Procedure setfiveflag(fivflg: integer);
Procedure setvisit(x: Boolean);
Procedure setwhere(x, y: integer);
Procedure getname(result_name: String);
Procedure getaddr(result_addr: String);
Procedure getcode(result_code: String);
Function getflag(): integer;
Function getfiveflag(): integer;
Function getxpoint(): integer;
Function getypoint(): integer;
Procedure changeimg(x, y: integer; MCanvas: TCanvas);
Procedure setstatu(); Virtual;
Procedure print(x, y: integer; MCanvas: TCanvas); Virtual;
Procedure put_img(x, y: integer; MCanvas: TCanvas); Virtual;
Function testaddr(): integer; Virtual;
End;

我以为经过上面的语句之后point指针里面应该保存LD的类指针,但是并没有像我想的那样依然是基类TIOPOINT的指针.因为不同的类LD,LDI的具体执行函数是不同的.各位谁能帮忙一下!
 
你到以下地址去找找,可能找得到你需要的答案。
http://iinsnian.cnblogs.com/
这地址里介绍了很多DELPHI的技巧,不知道你的运气好不好。
 
只有声明为虚函数或动态函数的函数才具有多态性。不知你哪个函数执行的不对?
 
没发现这个问题.可以正常工作
type
PC1 = ^C1;
C1 = Class(TObject)
private
protected
fName:String;
public
function GetName():String;virtual;
Constructor Create(name:String);virtual;
end;
C2 = Class(C1)
private
protected
public
function GetName():String;override;
Constructor Create(name:String);override ;
end;
............................
var
Form1: TForm1;
p:PC1;
implementation

{$R *.dfm}
function C1.GetName():String;
begin
Result:=fname;
end;
Constructor C1.Create(name:String);
begin
fName:=name;
end;

function C2.GetName():String;
begin
Result:='C2=='+fName;
end;
Constructor C2.Create(name:String);
begin
inherited Create(name);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin

p^:=C1.Create('test');
ShowMessage(p^.GetName);
p^.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
p^:=C2.Create('test');
ShowMessage(p^.GetName);
p^.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
new(p);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
p^:=C2.Create('test2');
ShowMessage(p^.GetName);
p^.Free;
end;
 
后退
顶部