I
iforward
Unregistered / Unconfirmed
GUEST, unregistred user!
这些天正在看<<delphi面象对象编程>>
对象在内存中的首4个字节应该是存方指向虚拟表的指针,但我更改了其指向
虚方法表的指针后,却没效果,有人知道这是为什么吗?
谢谢,(本人还有一百多点分了,就少给点了)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMan = class ;
TChinese = class;
PTData = ^TData;
TData = record
Name:string;//名字
Age:Integer;//年龄
Sex:Boolean;//true为男人,false为女人
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FPerson:TMan;
FPointer;
FPIntInteger;
FPDataTData;
FPstrChar;
public
{ Public declarations }
end;
TMan = class
private
Name:string;//名字
Age:Integer;//年龄
Sex:Boolean;//true为男人,false为女人
public
constructor Create();
function ShowName():string;virtual;
function ShowAge():integer;virtual;
end;
TChinese = class(TMan)
private
Language:string;
public
constructor Create();
function ShowName():string;override;
function ShowAge():integer;override;
end;
var
Form1: TForm1;
implementation
constructor TMan.Create;
begin
Name:='No Name';
Age :=100;
sex :=true;
end;
function TMan.ShowAge: integer;
begin
result:=self.Age;
end;
function TMan.ShowName: string;
begin
result:=self.Name;
end;
{ TChinese }
constructor TChinese.Create;
begin
Name:='fangchunfei';
Age:=15;
sex:=true;
end;
function TChinese.ShowAge: integer;
begin
result:=self.Age;
end;
function TChinese.ShowName: string;
begin
result:=self.Name;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
P,PTempointer;
N,M:integer;
FTemPerson:TMan;
begin
Memo1.Lines.Clear;
FPerson:=TChinese.Create;
Memo1.Lines.Add('Name:'+FPerSon.ShowName);
Memo1.Lines.Add('Age:'+ IntToStr(FPerSon.ShowAge));
FP:=@FPerson;
P:=FP;
P:=Pointer(P^);
N:=Integer(P);
Memo1.Lines.Add('此时FPerson为指向TChinese对象');
Memo1.Lines.Add('FPerson对象首地址: '+IntToHex(N,8)) ;
N:=Integer(P^);
Memo1.Lines.Add('FPerson中虚表的地址:'+ IntToHex(N,8));
//强行改变FPerson对象中指向的虚表指针,让其指向父类对象中的虚表
FTemPerson:=TMan.Create;
PTemp:=Pointer(FTemPerson);
M:=Integer((PInteger(PTemp))^);
N:=Integer(PTemp);
N:=N+4;
Memo1.Lines.Add('FTemPerson对象中虚表的地址: '+IntToHex(M,8)) ;
FP:=Pointer(FPerson);
P:=FP;
N:=Integer(P);
N:=N+4;
FPData:=PTData(N);
Memo1.Lines.Add('FPData 测试,FPData.Name : '+FPData^.Name);
Memo1.Lines.Add('FTemPerson对象中虚表的地址: '+IntToHex(M,8)) ;
Memo1.Lines.Add('改变前FPerson中指向虚表的地址:'+ IntToHex(Integer(P^),8));
Integer(P^):=M;
Memo1.Lines.Add('改变后FPerson中指向虚表的地址:'+ IntToHex(Integer(P^),8));
Edit3.Text:=FPerson.ShowName();//却是没有调用父类中ShowName,为什么啊?
N:=N+4;
PInteger(N)^:=200;
Edit4.Text:=IntTostr(PInteger(N)^);
Edit5.Text:=IntTostr(FPerson.showage);
FPerson.Free;
FTemPerson.Free;
end;
end.
对象在内存中的首4个字节应该是存方指向虚拟表的指针,但我更改了其指向
虚方法表的指针后,却没效果,有人知道这是为什么吗?
谢谢,(本人还有一百多点分了,就少给点了)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMan = class ;
TChinese = class;
PTData = ^TData;
TData = record
Name:string;//名字
Age:Integer;//年龄
Sex:Boolean;//true为男人,false为女人
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FPerson:TMan;
FPointer;
FPIntInteger;
FPDataTData;
FPstrChar;
public
{ Public declarations }
end;
TMan = class
private
Name:string;//名字
Age:Integer;//年龄
Sex:Boolean;//true为男人,false为女人
public
constructor Create();
function ShowName():string;virtual;
function ShowAge():integer;virtual;
end;
TChinese = class(TMan)
private
Language:string;
public
constructor Create();
function ShowName():string;override;
function ShowAge():integer;override;
end;
var
Form1: TForm1;
implementation
constructor TMan.Create;
begin
Name:='No Name';
Age :=100;
sex :=true;
end;
function TMan.ShowAge: integer;
begin
result:=self.Age;
end;
function TMan.ShowName: string;
begin
result:=self.Name;
end;
{ TChinese }
constructor TChinese.Create;
begin
Name:='fangchunfei';
Age:=15;
sex:=true;
end;
function TChinese.ShowAge: integer;
begin
result:=self.Age;
end;
function TChinese.ShowName: string;
begin
result:=self.Name;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
P,PTempointer;
N,M:integer;
FTemPerson:TMan;
begin
Memo1.Lines.Clear;
FPerson:=TChinese.Create;
Memo1.Lines.Add('Name:'+FPerSon.ShowName);
Memo1.Lines.Add('Age:'+ IntToStr(FPerSon.ShowAge));
FP:=@FPerson;
P:=FP;
P:=Pointer(P^);
N:=Integer(P);
Memo1.Lines.Add('此时FPerson为指向TChinese对象');
Memo1.Lines.Add('FPerson对象首地址: '+IntToHex(N,8)) ;
N:=Integer(P^);
Memo1.Lines.Add('FPerson中虚表的地址:'+ IntToHex(N,8));
//强行改变FPerson对象中指向的虚表指针,让其指向父类对象中的虚表
FTemPerson:=TMan.Create;
PTemp:=Pointer(FTemPerson);
M:=Integer((PInteger(PTemp))^);
N:=Integer(PTemp);
N:=N+4;
Memo1.Lines.Add('FTemPerson对象中虚表的地址: '+IntToHex(M,8)) ;
FP:=Pointer(FPerson);
P:=FP;
N:=Integer(P);
N:=N+4;
FPData:=PTData(N);
Memo1.Lines.Add('FPData 测试,FPData.Name : '+FPData^.Name);
Memo1.Lines.Add('FTemPerson对象中虚表的地址: '+IntToHex(M,8)) ;
Memo1.Lines.Add('改变前FPerson中指向虚表的地址:'+ IntToHex(Integer(P^),8));
Integer(P^):=M;
Memo1.Lines.Add('改变后FPerson中指向虚表的地址:'+ IntToHex(Integer(P^),8));
Edit3.Text:=FPerson.ShowName();//却是没有调用父类中ShowName,为什么啊?
N:=N+4;
PInteger(N)^:=200;
Edit4.Text:=IntTostr(PInteger(N)^);
Edit5.Text:=IntTostr(FPerson.showage);
FPerson.Free;
FTemPerson.Free;
end;
end.