qual a função do override ???

Delphi

27/12/2006

ja vi mas não entendi


Arc

Arc

Curtidas 0

Respostas

Adriano Santos

Adriano Santos

27/12/2006

O override é usado para sobrescrever um método. Simples assim.
Porém, somente métodos virtuais ou dinâmicos podem ser sobrescritos.

Digamos que você tenha uma classe como abaixo que contenha o método Draw, que desenha uma figura. Porém a classe contém outras ´sub-classes´ que desenham figuras diferentes (círculo, quadrado e etc). Você pode criar diversas subclasses e usar o nome nome de método pra cada uma, mas desempenhando papéis diferentes. Neste caso você precisa sobrescrever o método [i:1532d7e103]Draw[/i:1532d7e103] original.


Num sei se fui claro.

[quote:1532d7e103=´Help do Delphi´]
To make a method virtual or dynamic, include the virtual or dynamic directive in its declaration. Virtual and dynamic methods, unlike static methods, can be overridden in descendant classes. When an overridden method is called, the actual (runtime) type of the class or object used in the method call—not the declared type of the variable—determines which implementation to activate.
To override a method, redeclare it with the override directive. An override declaration must match the ancestor declaration in the order and type of its parameters and in its result type (if any).

In the following example, the Draw method declared in TFigure is overridden in two descendant classes.

type

TFigure = class
procedure Draw; virtual;
end;
TRectangle = class(TFigure)
procedure Draw; override;
end;
TEllipse = class(TFigure)
procedure Draw; override;
end;

Given these declarations, the following code illustrates the effect of calling a virtual method through a variable whose actual type varies at runtime.

var

Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw; // calls TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw; // calls TEllipse.Draw
Figure.Destroy;
end;

Only virtual and dynamic methods can be overridden. All methods, however, can be overloaded; see Overloading methods.

Virtual versus dynamic

Virtual and dynamic methods are semantically equivalent. They differ only in the implementation of method-call dispatching at runtime. Virtual methods optimize for speed, while dynamic methods optimize for code size.
In general, virtual methods are the most efficient way to implement polymorphic behavior. Dynamic methods are useful when a base class declares many overridable methods which are inherited by many descendant classes in an application, but only occasionally overridden.

Overriding versus hiding
Reintroduce
Abstract methods
[/quote:1532d7e103]


GOSTEI 0
POSTAR