Override:Directive Defines a method that replaces a virtual parent class method

Description
The Override directive defines a class method as overriding the same named method in a parent class.
 
For example, you might want to override (replace) the operation of the constructor to take into account the class changes introduced by your class.
 
You can only override classes defined as virtual or dynamic (the latter are beyond the scope of Delphi basics).
 
Only those methods that can be sensibly overriden by a derived class are normally allowed to do so.
 
If a method is marked as abstract as well as virtual then you must override it and implement it for it to be useable by a user of your class.
 
 
Example code : Overriding abstract methods
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit  Unit1 ;

interface

uses
   Forms ,  Dialogs ,  Classes ,  Controls ,  StdCtrls ,  SysUtils ;

type
   // Define a base TPolygon class :
   // This class is a triangle if 3 sides, square if 4 sides ...
   TPolygon  =  class
   private
     sideCount   :  Integer ;   // How many sides?
     sideLength  :  Integer ;   // How long each side?
     shapeArea   :  Double ;    // Area of the polygon
   protected
      procedure  setArea ;  Virtual ;  Abstract ;   // Cannot code until sides known
   published
     property  count   :  Integer  read  sideCount ;
     property  length  :  Integer  read  sideLength ;
     property  area    :  Double   read  shapeArea ;
     constructor  Create ( sides ,  length  :  Integer );
   end ;

   // Define triangle and square descendents
   TTriangle  =  class ( TPolygon )
   protected
     procedure  setArea ;  Override ;    // Override the abstract method
   end ;

   TSquare  =  class ( TPolygon )
   protected
     procedure  setArea ;  Override ;    // Override the abstract method
   end ;

   // Define the form class used by this unit
   TForm1  =  class ( TForm )
     procedure  FormCreate ( Sender :  TObject );
   private
     { Private declarations }
   public
     { Public declarations }
   end ;

var
   Form1 :  TForm1 ;

implementation
{$R *.dfm}  // Include form definitions

// Create the TPolygon object
constructor  TPolygon . Create ( sides ,  length  :  Integer );
begin
   // Save the number and length of the sides
   sideCount  :=  sides ;
   sideLength  :=  length ;

   // Set the area using the abstract setArea method :
   // This call will be satisfied only by a subclass
   setArea ;
end ;

// Implement the abstract setArea parent method for the triangle
procedure  TTriangle . setArea ;
begin
   // Calculate and save the area of the square
   shapeArea  :=  ( sideLength  *  sideLength )  /  2 ;
end ;

// Implement the abstract setArea parent method for the square
procedure  TSquare . setArea ;
begin
   // Calculate and save the area of the square
   shapeArea  :=  sideLength  *  sideLength ;
end ;

// Main line code
procedure  TForm1 . FormCreate ( Sender :  TObject );
var
   triangle  :  TTriangle ;
   square    :  TSquare ;
begin
   // Create a triangle and a square
   triangle  :=  TTriangle . Create ( 3 ,  10 );
   square    :=  TSquare . Create ( 4 ,  10 );

   // Show the areas of our polygons:
   ShowMessageFmt ( 'Triangle area = %f' ,[ triangle . area ]);
   ShowMessageFmt ( 'Square   area = %f' ,[ square . area ]);
end ;

end . 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值