1unit Unit1;
2
3interface
4
5type
6 TForm1 = class(TForm)
7 Button1: TButton;
8 Button2: TButton;
9 procedure Button1Click(Sender: TObject);
10 procedure Button2Click(Sender: TObject);
11 private
12 { Private declarations }
13 public
14 { Public declarations }
15 end;
16
17implementation
18 uses
19 Person, ET; //引用自定义的两个类
20procedure TForm1.Button1Click(Sender: TObject);
21 VAR
22 tulater: TPerson;
23begin
24 tulater := TPerson.Create; //Blood此时初始化为'O'
25 tulater.myName := '涂磊';
26 tulater.High := 156;
27 //tulater.DNA := 'asdfwefkooj3249rrj9';
28//上面一行错://Protected属性在此单元内(属性定义的单元)和子类属性定义的单元内可见 和直接访问使用 DNA
29
30 tulater.setDNA('asdfwefkooj3249rrj9');
31//只能以上面方法间接访问Protected属性,通过public的方法
32 // tulater.Blood := 'O';
33//上面一行错://Private属性只在Blood属性定义的单元内可见,和直接访问使用 Blood
34 ShowMessage(tulater.myName + #13#10 + '身高= ' + IntToStr(tulater.High) + #13#10 + '血型= ' + tulater.getBlood
35 + #13#10 + 'DNA= ' + tulater.getDNA);
36 tulater.Free;
37end;
38
39procedure TForm1.Button2Click(Sender: TObject);
40 VAR
41 ETfish: TET;
42begin
43 ETfish := TET.Create;
44 ETfish.myName := '外星鱼';
45 ETfish.High := 15;
46 //ETfish.DNA := 'asdfwe$%#%#%%Q#%%#%24#9rrj9';
47 ETfish.setDNA('asdfwe$%#%#%%Q#%%#%24#9rrj9');
48 // ETfish.Blood := 'O';
49 ShowMessage(ETfish.myName + #13#10 + '身高= ' + IntToStr(ETfish.High) + #13#10 +
50 '血型= ' + ETfish.getBlood + #13#10 +
51 'DNA= ' + ETfish.getDNA + #13#10 +
52 'UFOSpeed= ' + IntToStr(ETfish.getUFOSpeed) ) ;
53 ShowMessage('初始化时 UFOSpeed=' + IntToStr(ETfish.getUFOSpeed));
54 ETfish.setUFOSpeed(41458174);
55 ShowMessage('现在 UFOSpeed=' + IntToStr(ETfish.getUFOSpeed));
56
57 ETfish.Free;
58end;
59
60end.
61
62
2
3interface
4
5type
6 TForm1 = class(TForm)
7 Button1: TButton;
8 Button2: TButton;
9 procedure Button1Click(Sender: TObject);
10 procedure Button2Click(Sender: TObject);
11 private
12 { Private declarations }
13 public
14 { Public declarations }
15 end;
16
17implementation
18 uses
19 Person, ET; //引用自定义的两个类
20procedure TForm1.Button1Click(Sender: TObject);
21 VAR
22 tulater: TPerson;
23begin
24 tulater := TPerson.Create; //Blood此时初始化为'O'
25 tulater.myName := '涂磊';
26 tulater.High := 156;
27 //tulater.DNA := 'asdfwefkooj3249rrj9';
28//上面一行错://Protected属性在此单元内(属性定义的单元)和子类属性定义的单元内可见 和直接访问使用 DNA
29
30 tulater.setDNA('asdfwefkooj3249rrj9');
31//只能以上面方法间接访问Protected属性,通过public的方法
32 // tulater.Blood := 'O';
33//上面一行错://Private属性只在Blood属性定义的单元内可见,和直接访问使用 Blood
34 ShowMessage(tulater.myName + #13#10 + '身高= ' + IntToStr(tulater.High) + #13#10 + '血型= ' + tulater.getBlood
35 + #13#10 + 'DNA= ' + tulater.getDNA);
36 tulater.Free;
37end;
38
39procedure TForm1.Button2Click(Sender: TObject);
40 VAR
41 ETfish: TET;
42begin
43 ETfish := TET.Create;
44 ETfish.myName := '外星鱼';
45 ETfish.High := 15;
46 //ETfish.DNA := 'asdfwe$%#%#%%Q#%%#%24#9rrj9';
47 ETfish.setDNA('asdfwe$%#%#%%Q#%%#%24#9rrj9');
48 // ETfish.Blood := 'O';
49 ShowMessage(ETfish.myName + #13#10 + '身高= ' + IntToStr(ETfish.High) + #13#10 +
50 '血型= ' + ETfish.getBlood + #13#10 +
51 'DNA= ' + ETfish.getDNA + #13#10 +
52 'UFOSpeed= ' + IntToStr(ETfish.getUFOSpeed) ) ;
53 ShowMessage('初始化时 UFOSpeed=' + IntToStr(ETfish.getUFOSpeed));
54 ETfish.setUFOSpeed(41458174);
55 ShowMessage('现在 UFOSpeed=' + IntToStr(ETfish.getUFOSpeed));
56
57 ETfish.Free;
58end;
59
60end.
61
62
1unit ET; //TET类声明所在单元
2interface
3 uses
4 CLASSES,Dialogs,Person;
5TYPE
6 TET = CLASS(TPerson) //完全继承Person所有的属性和方法
7 private //加入子类新的属性和方法
8 UFOSpeed: Integer;
9 { Private declarations }
10 public
11 CONSTRUCTOR Create;
12 PROCEDURE setUFOSpeed(aSpeed: Longword);
13 FUNCTION getUFOSpeed: Longword;
14 { Public declarations }
15 end;
16implementation
17
18FUNCTION TET.getUFOSpeed: Longword;
19BEGIN
20 result := self.UFOSpeed; //读取Private 成员的值
21end;
22
23PROCEDURE TET.setUFOSpeed(aSpeed: Longword);
24BEGIN
25 self.UFOSpeed := aSpeed; //以参数设定Private 成员的值
26end;
27
28CONSTRUCTOR TET.Create;
29BEGIN
30 INHERITED Create; //调用直接父类Tperson的Create方法// ShowMessage('Tperson.Create 执行');
31 ShowMessage('TET.Create 执行');
32 Self.UFOSpeed := 300000000; //Private属性只在属性定义的单元内可见,只可以在这里这样直接访问使用 UFOSpeed
33end;
34VAR //为测试而设置此区
35 AET : TET;
36BEGIN
37 AET := TET.Create;
38 AET.DNA := 'fdsafsrfwefsd$#^^3';//父类的Protected属性在子类单元(本单元)内可见 ,在这里这样直接访问使用
39 //AET.Blood := 'A'; //父类的Private属性在子类单元(本单元)内不可见(只在其属性定义的单元内可见) ,在这里这样不能直接访问使用
40 ShowMessage('AET.DNA = ' + AET.DNA );
41 AET.Free;
42end.
43end.
44
45
2interface
3 uses
4 CLASSES,Dialogs,Person;
5TYPE
6 TET = CLASS(TPerson) //完全继承Person所有的属性和方法
7 private //加入子类新的属性和方法
8 UFOSpeed: Integer;
9 { Private declarations }
10 public
11 CONSTRUCTOR Create;
12 PROCEDURE setUFOSpeed(aSpeed: Longword);
13 FUNCTION getUFOSpeed: Longword;
14 { Public declarations }
15 end;
16implementation
17
18FUNCTION TET.getUFOSpeed: Longword;
19BEGIN
20 result := self.UFOSpeed; //读取Private 成员的值
21end;
22
23PROCEDURE TET.setUFOSpeed(aSpeed: Longword);
24BEGIN
25 self.UFOSpeed := aSpeed; //以参数设定Private 成员的值
26end;
27
28CONSTRUCTOR TET.Create;
29BEGIN
30 INHERITED Create; //调用直接父类Tperson的Create方法// ShowMessage('Tperson.Create 执行');
31 ShowMessage('TET.Create 执行');
32 Self.UFOSpeed := 300000000; //Private属性只在属性定义的单元内可见,只可以在这里这样直接访问使用 UFOSpeed
33end;
34VAR //为测试而设置此区
35 AET : TET;
36BEGIN
37 AET := TET.Create;
38 AET.DNA := 'fdsafsrfwefsd$#^^3';//父类的Protected属性在子类单元(本单元)内可见 ,在这里这样直接访问使用
39 //AET.Blood := 'A'; //父类的Private属性在子类单元(本单元)内不可见(只在其属性定义的单元内可见) ,在这里这样不能直接访问使用
40 ShowMessage('AET.DNA = ' + AET.DNA );
41 AET.Free;
42end.
43end.
44
45