S
Steven_Jason
Unregistered / Unconfirmed
GUEST, unregistred user!
父类的private成员在子类(前提是与父类在同一单元文件中)中也是可见的呀!?怎么Delphi这么怪啊!?欢迎大家参与讨论啊!Delphi的设计者们是怎么考虑的啊? ( 积分: 30 )<br />以下程序中父类(TClass1)的private成员aa在子类TClass2中是可见的!!!
怎么Delphi这么怪啊!!
unit VisibilityUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tclass1=class
private
aa:integer;
public
function test(a:integer):integer;
constructor create(a:integer);
end;
type
Tclass2=class(Tclass1)
// private
// aa:integer;
public
function test(a:integer):integer;
constructor create(a:integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor Tclass1.create(a:integer);
begin
aa:=a;
end;
constructor Tclass2.create(a:integer);
begin
//aa:=a;
inherited;
end;
function Tclass1.test(a:integer):integer;
begin
result:=a*2;
end;
function Tclass2.test(a:integer):integer;
begin
result:=a*4 ;
// result := inherited test1(a);
// inherited ;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
class1:Tclass1;
class2:Tclass2;
// class3:Tclass3;
begin
if RadioGroup1.ItemIndex=0 then
begin
class1:=Tclass1.create(1);
showmessage('The private mumber ''aa ''' + '= ' +
inttostr(class1.test(class1.aa)) + ' .' + #13 +
'The size in bytes of the object class1 are ' +
inttostr(class1.InstanceSize) + ' bytes .');
end
else if RadioGroup1.ItemIndex=1 then
begin
class2 := Tclass2.create(1);
// class1 := Tclass1.create(1);
showmessage('The private mumber ''aa ''' + ' inherited aa' + '= ' +
inttostr(class2.test(class2.aa)) + ' .' + #10 +
'The size in bytes of the object class2 are ' +
inttostr(class2.InstanceSize) + ' bytes .');
end;
end;
end.
怎么Delphi这么怪啊!!
unit VisibilityUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tclass1=class
private
aa:integer;
public
function test(a:integer):integer;
constructor create(a:integer);
end;
type
Tclass2=class(Tclass1)
// private
// aa:integer;
public
function test(a:integer):integer;
constructor create(a:integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor Tclass1.create(a:integer);
begin
aa:=a;
end;
constructor Tclass2.create(a:integer);
begin
//aa:=a;
inherited;
end;
function Tclass1.test(a:integer):integer;
begin
result:=a*2;
end;
function Tclass2.test(a:integer):integer;
begin
result:=a*4 ;
// result := inherited test1(a);
// inherited ;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
class1:Tclass1;
class2:Tclass2;
// class3:Tclass3;
begin
if RadioGroup1.ItemIndex=0 then
begin
class1:=Tclass1.create(1);
showmessage('The private mumber ''aa ''' + '= ' +
inttostr(class1.test(class1.aa)) + ' .' + #13 +
'The size in bytes of the object class1 are ' +
inttostr(class1.InstanceSize) + ' bytes .');
end
else if RadioGroup1.ItemIndex=1 then
begin
class2 := Tclass2.create(1);
// class1 := Tclass1.create(1);
showmessage('The private mumber ''aa ''' + ' inherited aa' + '= ' +
inttostr(class2.test(class2.aa)) + ' .' + #10 +
'The size in bytes of the object class2 are ' +
inttostr(class2.InstanceSize) + ' bytes .');
end;
end;
end.