1type
2 TOldDog = class(TDog)
3 public
4 PROCEDURE Bark(Timens: Integer);OVERRIDE; //子类 TOldDog覆盖父类TDog方法 Bark
5 //变量个数必须对应,但每个变量名可以不与父类方法一样
6 { Public declarations }
7 end;
8type
9 TLittleDog = class(TDog)
10 public
11 PROCEDURE Barkm;
12//[Warning] : Method 'Bark' hides virtual method of base type 'TDog'
13//这是子类重新定义,跟父类的Bark无任何关系,所以会把父类的Bark方法隐藏(Hiding)
14//若其后加reintroduce修饰符可去除编译的警告
15//[Warning] Unit1.pas(31): Method 'Bark' hides virtual method of base type 'TDog'
16 { Public declarations }
17 end;
18PROCEDURE TLittleDog.Barkm;//TLittleDog的Bark方法的实现
19BEGIN
20 ShowMessage('嗷嗷~~~邀邀');
21end;
22
23PROCEDURE TOldDog.Bark(Timens: Integer); //TOldDog的Bark方法的实现
24BEGIN
25 ShowMessage('嗷~~~呜~~~' + #13 + #13 +' 给我 ' + IntToStr(Timens) + ' 根骨头');
26end;
27
28PROCEDURE TDog.Bark(Times: Integer); //TDog的Bark方法的实现
29VAR
30 i: Integer;
31BEGIN
32 i := Times;
33 IF i > 5 THEN
34 ShowMessage('没力,叫不出来!')
35 else
36 FOR I := 1 TO Times DO // Iterate
37 ShowMessage('汪~~ 第 ' + IntToStr(i) + ' 声');
38 // for
39end;
40
41procedure TForm1.Button1Click(Sender: TObject);
42VAR
43 SmallWhite: TDog;
44 bkTimes: Integer;
45begin
46 SmallWhite := TDog.Create; //建立TDog的对象
47 SmallWhite.Name := '小白';
48 Edit1.Text := '狗狗叫做 ' + SmallWhite.Name;
49 bkTimes := StrToInt(InputBox('要 ' + SmallWhite.Name + '叫几声?', '输入次数(限整数)', '3'));
50 SmallWhite.Bark(bkTimes); //调用的是TDog的Bark方法
51 SmallWhite.Free; //析构TDog的对象
52end;
53
54procedure TForm1.Button2Click(Sender: TObject);
55VAR
56 OldBlack: TOldDog;
57begin
58 OldBlack := TOldDog.Create; //建立TOldDog的对象
59 OldBlack.Name := '老黑';
60 Edit1.Text := '老狗叫做 ' + OldBlack.Name;
61 OldBlack.Bark(6); //调用的是TOldDog覆盖后的Bark方法
62 OldBlack.Free; //析构TOldDog的对象
63end;
64
65procedure TForm1.Button3Click(Sender: TObject);
66VAR
67 Spot: TLittleDog;
68begin
69 Spot := TLittleDog.Create; //建立TLittleDog的对象
70 Spot.Name := '小花';
71 Edit1.Text := '小狗叫做 ' + Spot.Name;
72 Spot.Barkm; //调用的是TLittleDog重新定义的Bark方法
73 Spot.Free; //析构TLittleDog的对象
74end;
75
76procedure TForm1.Button4Click(Sender: TObject);
77VAR
78 aDog: TDog; //定义为TDog类的对象变量
79begin
80 aDog := TOldDog.Create; //用子类TOldDog建立父类TDog的对象
81 Label1.Caption := 'aDog 参考ToldDog对象';
82 aDog.Bark(2); //调用的是TOldDog的Bark方法
83 aDog.Free; //析构TOldDog的对象
84
85 aDog := TLittleDog.Create; //用子类TLittleDog建立父类TDog的对象
86 Label1.Caption := 'aDog 参考TLittleDog对象';
87 aDog.Bark(2); //调用的是父类TDog的Bark方法
88 // aDog.Barkm; //无法Bind(绑定到)TLittleDog的Bark方法 , 因为跟 父类TDog无任何关系.
89 aDog.Free; //析构TDog的对象
90
91end;
92
93
2 TOldDog = class(TDog)
3 public
4 PROCEDURE Bark(Timens: Integer);OVERRIDE; //子类 TOldDog覆盖父类TDog方法 Bark
5 //变量个数必须对应,但每个变量名可以不与父类方法一样
6 { Public declarations }
7 end;
8type
9 TLittleDog = class(TDog)
10 public
11 PROCEDURE Barkm;
12//[Warning] : Method 'Bark' hides virtual method of base type 'TDog'
13//这是子类重新定义,跟父类的Bark无任何关系,所以会把父类的Bark方法隐藏(Hiding)
14//若其后加reintroduce修饰符可去除编译的警告
15//[Warning] Unit1.pas(31): Method 'Bark' hides virtual method of base type 'TDog'
16 { Public declarations }
17 end;
18PROCEDURE TLittleDog.Barkm;//TLittleDog的Bark方法的实现
19BEGIN
20 ShowMessage('嗷嗷~~~邀邀');
21end;
22
23PROCEDURE TOldDog.Bark(Timens: Integer); //TOldDog的Bark方法的实现
24BEGIN
25 ShowMessage('嗷~~~呜~~~' + #13 + #13 +' 给我 ' + IntToStr(Timens) + ' 根骨头');
26end;
27
28PROCEDURE TDog.Bark(Times: Integer); //TDog的Bark方法的实现
29VAR
30 i: Integer;
31BEGIN
32 i := Times;
33 IF i > 5 THEN
34 ShowMessage('没力,叫不出来!')
35 else
36 FOR I := 1 TO Times DO // Iterate
37 ShowMessage('汪~~ 第 ' + IntToStr(i) + ' 声');
38 // for
39end;
40
41procedure TForm1.Button1Click(Sender: TObject);
42VAR
43 SmallWhite: TDog;
44 bkTimes: Integer;
45begin
46 SmallWhite := TDog.Create; //建立TDog的对象
47 SmallWhite.Name := '小白';
48 Edit1.Text := '狗狗叫做 ' + SmallWhite.Name;
49 bkTimes := StrToInt(InputBox('要 ' + SmallWhite.Name + '叫几声?', '输入次数(限整数)', '3'));
50 SmallWhite.Bark(bkTimes); //调用的是TDog的Bark方法
51 SmallWhite.Free; //析构TDog的对象
52end;
53
54procedure TForm1.Button2Click(Sender: TObject);
55VAR
56 OldBlack: TOldDog;
57begin
58 OldBlack := TOldDog.Create; //建立TOldDog的对象
59 OldBlack.Name := '老黑';
60 Edit1.Text := '老狗叫做 ' + OldBlack.Name;
61 OldBlack.Bark(6); //调用的是TOldDog覆盖后的Bark方法
62 OldBlack.Free; //析构TOldDog的对象
63end;
64
65procedure TForm1.Button3Click(Sender: TObject);
66VAR
67 Spot: TLittleDog;
68begin
69 Spot := TLittleDog.Create; //建立TLittleDog的对象
70 Spot.Name := '小花';
71 Edit1.Text := '小狗叫做 ' + Spot.Name;
72 Spot.Barkm; //调用的是TLittleDog重新定义的Bark方法
73 Spot.Free; //析构TLittleDog的对象
74end;
75
76procedure TForm1.Button4Click(Sender: TObject);
77VAR
78 aDog: TDog; //定义为TDog类的对象变量
79begin
80 aDog := TOldDog.Create; //用子类TOldDog建立父类TDog的对象
81 Label1.Caption := 'aDog 参考ToldDog对象';
82 aDog.Bark(2); //调用的是TOldDog的Bark方法
83 aDog.Free; //析构TOldDog的对象
84
85 aDog := TLittleDog.Create; //用子类TLittleDog建立父类TDog的对象
86 Label1.Caption := 'aDog 参考TLittleDog对象';
87 aDog.Bark(2); //调用的是父类TDog的Bark方法
88 // aDog.Barkm; //无法Bind(绑定到)TLittleDog的Bark方法 , 因为跟 父类TDog无任何关系.
89 aDog.Free; //析构TDog的对象
90
91end;
92
93