Programação
Orientada a Objeto - Parte IV
Um
exemplo prático em Delphi
Exemplo de POO na Linguagem Delphi (cont.)
Vamos tornar os métodos da nossa superclasse em métodos virtuais:
|
01 |
unit
UClasses; |
|
02 |
|
|
03 |
interface |
|
04 |
|
|
05 |
type |
|
06 |
TPessoa = class |
|
07 |
Nome: string; |
|
08 |
Fone: string; |
|
09 |
procedure Avaliar; virtual; |
|
10 |
constructor Create; virtual; |
|
11 |
destructor Destroy; virtual; |
|
12 |
end; |
|
13 |
|
|
14 |
TAluno = class(TPessoa) |
|
15 |
Turma: string; |
|
16 |
procedure Avaliar; override; |
|
17 |
constructor Create; override; |
|
18 |
destructor Destroy; override; |
|
19 |
end; |
|
20 |
|
|
21 |
TProf = class(TPessoa) |
|
22 |
Disciplina: string; |
|
23 |
procedure Avaliar; override; |
|
24 |
constructor Create; override; |
|
25 |
destructor Destroy; override; |
|
26 |
end; |
|
27 |
|
|
28 |
implementation |
|
29 |
|
|
30 |
uses
Dialogs; |
|
31 |
|
|
32 |
{
TPessoa } |
|
33 |
|
|
34 |
procedure
TPessoa.Avaliar; |
|
35 |
begin |
|
36 |
ShowMessage('Avaliação sendo executada'); |
|
37 |
end; |
|
38 |
|
|
39 |
constructor
TPessoa.Create; |
|
40 |
begin |
|
41 |
Self.Nome := 'Novo Nome'; |
|
42 |
Self.Fone := 'Novo Fone'; |
|
43 |
end; |
|
44 |
|
|
45 |
destructor
TPessoa.Destroy; |
|
46 |
begin |
|
47 |
Self.Nome := ''; |
|
48 |
Self.Fone := ''; |
|
49 |
end; |
|
50 |
|
|
51 |
{
TAluno } |
|
52 |
|
|
53 |
procedure
TAluno.Avaliar; |
|
54 |
begin |
|
55 |
inherited; |
|
56 |
ShowMessage('Respondendo a prova'); |
|
57 |
end; |
|
58 |
|
|
59 |
constructor
TAluno.Create; |
|
60 |
begin |
|
61 |
inherited; |
|
62 |
Self.Turma := 'Nova Turma'; |
|
63 |
end; |
|
64 |
|
|
65 |
destructor
TAluno.Destroy; |
|
66 |
begin |
|
67 |
Self.Turma := ''; |
|
68 |
inherited; |
|
69 |
end; |
|
70 |
|
|
71 |
{
TProf } |
|
72 |
|
|
73 |
procedure
TProf.Avaliar; |
|
74 |
begin |
|
75 |
inherited; |
|
76 |
ShowMessage('Aplicando a prova'); |
|
77 |
end; |
|
78 |
|
|
79 |
constructor
TProf.Create; |
|
80 |
begin |
|
81 |
inherited; |
|
82 |
Self.Disciplina := 'Nova Disciplina'; |
|
83 |
end; |
|
84 |
|
|
85 |
destructor
TProf.Destroy; |
|
86 |
begin |
|
87 |
Self.Disciplina := ''; |
|
88 |
inherited; |
|
89 |
end; |
|
90 |
|
|
91 |
end. |
Vemos uma das vantagens da POO, mudamos as classes mas não precisamos mudar nada no FPrincipal, para as alterações entrarem em vigor apenas compile e teste.
O código a seguir transforma a superclasse TPessoa em uma classe abstrata, obrigando as subclasses TAluno e TProf a implementar o método abstrato:
|
01 |
type |
|
02 |
TPessoa = class |
|
03 |
Nome: string; |
|
04 |
Fone: string; |
|
05 |
procedure Avaliar; virtual; |
|
06 |
procedure Lancar_Nota; virtual; abstract; |
|
07 |
constructor Create; virtual; |
|
08 |
destructor Destroy; virtual; |
|
09 |
end; |
|
10 |
|
|
11 |
TAluno = class(TPessoa) |
|
12 |
Turma: string; |
|
13 |
procedure Avaliar; override; |
|
14 |
procedure Lancar_Nota; override; |
|
15 |
constructor Create; override; |
|
16 |
destructor Destroy; override; |
|
17 |
end; |
|
18 |
|
|
19 |
TProf = class(TPessoa) |
|
20 |
Disciplina: string; |
|
21 |
procedure Avaliar; override; |
|
22 |
procedure Lancar_Nota; override; |
|
23 |
constructor Create; override; |
|
24 |
destructor Destroy; override; |
|
25 |
end; |
Pressione Crtl+Shift+C dentro das classes TAluno e TProf, em seguida entre com código mostrado:
|
01 |
procedure
TAluno.Lancar_Nota; |
|
02 |
var |
|
03 |
Nota: string; |
|
04 |
begin |
|
05 |
InputQuery('Nota', 'Qual foi a nota?',
Nota); |
|
06 |
end; |
|
07 |
|
|
08 |
procedure
TProf.Lancar_Nota; |
|
09 |
var |
|
10 |
Aluno, Nota: string; |
|
11 |
begin |
|
12 |
InputQuery('Aluno', 'Nome do aluno?',
Aluno); |
|
13 |
InputQuery('Nota', 'Qual foi a nota do
aluno ' + |
|
14 |
Aluno + '?', Nota); |
|
15 |
end; |
Mude o evento OnClick do TButton que está FPrincipal, compile, teste e verifique o erro que dá:
|
01 |
procedure TFPrincipal.Button1Click(Sender: TObject); |
|
02 |
var |
|
03 |
Pessoa: TPessoa; |
|
04 |
Aluno: TAluno; |
|
05 |
Prof: TProf; |
|
06 |
begin |
|
07 |
// Trabalhando com o objeto Pessoa |
|
08 |
Pessoa := TPessoa.Create; |
|
09 |
ShowMessage( 'Objeto Pessoa' +#13+ |
|
10 |
Pessoa.Nome +#13+ Pessoa.Fone ); |
|
11 |
Pessoa.Nome := 'Tadeu'; |
|
12 |
Pessoa.Fone := '1111-1111'; |
|
13 |
Pessoa.Avaliar; |
|
14 |
Pessoa.Lancar_Nota; |
|
15 |
ShowMessage( 'Objeto Pessoa' +#13+ |
|
16 |
Pessoa.Nome +#13+ Pessoa.Fone ); |
|
17 |
Pessoa.Destroy; |
|
18 |
|
|
19 |
// Trabalhando com o objeto Aluno |
|
20 |
Aluno := TAluno.Create; |
|
21 |
ShowMessage( 'Objeto Aluno' +#13+ |
|
22 |
Aluno.Nome +#13+ Aluno.Fone +#13+ Aluno.Turma
); |
|
23 |
Aluno.Nome := 'José'; |
|
24 |
Aluno.Fone := '2222-2222'; |
|
25 |
Aluno.Turma := 'Comp02'; |
|
26 |
Aluno.Avaliar; |
|
27 |
Aluno.Lancar_Nota; |
|
28 |
ShowMessage( 'Objeto Aluno' +#13+ |
|
29 |
Aluno.Nome +#13+ Aluno.Fone +#13+
Aluno.Turma ); |
|
30 |
Aluno.Destroy; |
|
31 |
|
|
32 |
// Trabalhando com o objeto Prof |
|
33 |
Prof := TProf.Create; |
|
34 |
ShowMessage( 'Objeto Prof' +#13+ |
|
35 |
Prof.Nome +#13+ Prof.Fone +#13+
Prof.Disciplina ); |
|
36 |
Prof.Nome := 'Maria'; |
|
37 |
Prof.Fone := '3333-3333'; |
|
38 |
Prof.Disciplina := 'Linguagem II'; |
|
39 |
Prof.Avaliar; |
|
40 |
Prof.Lancar_Nota; |
|
41 |
ShowMessage( 'Objeto Prof' +#13+ |
|
42 |
Prof.Nome +#13+ Prof.Fone +#13+
Prof.Disciplina ); |
|
43 |
Prof.Destroy; |
|
44 |
end; |
A classe TPessoa não poderia ter sido instanciado pois ela é uma classe abstrata, mas caso se deseje utilizá-la e desprezar a regra de não poder instanciar uma classe abstrata, basta comentar a linha 14 com a barra dupla.
|
Este artigo foi cedido por http://www.cassic.com.br/ |